Using the manage_posts_columns or manage_$post_type_posts_columns filter seems simple enough, right? But after you do something like the suggested example from the Codex, you discover that it doesn’t allow you to change the default column order in the WordPress editor screen.

function add_sticky_column( array $columns ) : array { $columns['sticky'] = __('Sticky'); return $columns; } add_filter( 'manage_posts_columns' , 'add_sticky_column' );
Code language: PHP (php)

You’ll find out that adding it to the array this way leaves it in the last position.

The Problem with Internet Answers

Many answers in StackOverflow and some tutorial sites suggest you to loop via foreach() through the $columns array. But you probably think that something like that increases the complexity of your code without a good reason. And you are right. So how to solve this in an elegant, efficient way?

Splitting the Array: the Problem with the Intuitive Solution to Change the Order of Columns in WordPress

It’s actually very simple. You may think about using the array_splice() function to insert it at a specific index, but this is not the best approach. For example, to insert the column at the index #3 in the array, you would do:

function add_sticky_column( array $columns ) : array { $new_columns[ 'sticky' => __('Sticky') ]; array_splice( $columns, 2, 0, $new_columns ); return $columns; } add_filter( 'manage_posts_columns' , 'add_sticky_column' );
Code language: PHP (php)

While this looks fine, the problem is that array_splice() resets the array keys from $new_columns. This is very inconvenient since that very key is what generates the column id and class. More importantly, you need that key to insert content in the column cells with manage_posts_custom_column.

Splitting the Array the Right Way

In order to preserve it, there’s an easy solution: break the $columns array in two and insert the new one in between taking advantage of the union (+) operator:

function add_sticky_column( array $columns ) : array { $new_columns[ 'sticky' => __('Sticky') ]; $columns = array_slice( $columns, 0, 2, true ) + $new_columns + array_slice( $columns, 2, 0, null, true); return $columns; } add_filter( 'manage_posts_columns' , 'add_sticky_column' );
Code language: PHP (php)

In conclusion:

  • array_slice() allows you to break the original array in two at a specific point
  • Thus, you can modify the order of your custom columns in the post editor.
  • This way avoids increasing the complexity of your code with an unnecessary foreach() loop.
  • It also preserves the array keys, while array_splice() doesn’t, which is crucial to preserve the identity of the column
  • This way, you can easily change the order of your column in the WordPress editor.