Change order of custom columns for edit panels

The question:

When you register a custom column like so:

//Register thumbnail column for au-gallery type
add_filter('manage_edit-au-gallery_columns', 'thumbnail_column');
function thumbnail_column($columns) {
$columns['thumbnail'] = 'Thumbnail';
return $columns;
}

by default it appears as the last one on the right.
How can I change the order?
What if I want to show the above column as the first one or the second one?

Thank you in advance

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You are basically asking a PHP question, but I’ll answer it because it’s in the context of WordPress. You need to rebuild the columns array, inserting your column before the column you want it to be left of:

add_filter('manage_posts_columns', 'thumbnail_column');
function thumbnail_column($columns) {
  $new = array();
  foreach($columns as $key => $title) {
    if ($key=='author') // Put the Thumbnail column before the Author column
      $new['thumbnail'] = 'Thumbnail';
    $new[$key] = $title;
  }
  return $new;
}

Method 2

If you have plugins like WPML which automatically add columns, even to custom post types, you may have complicated code in your table header.

You don’t want to copy the code to your column definition. Why would anyone, for that matter.

We just want to extend the already provided, nicely formatted and sortable default columns.

In fact, this is just seven lines of code, and it keeps all other columns intact.

# hook into manage_edit-<mycustomposttype>_columns
add_filter( 'manage_edit-mycustomposttype_columns', 'mycustomposttype_columns_definition' ) ;

# column definition. $columns is the original array from the admin interface for this posttype.
function mycustomposttype_columns_definition( $columns ) {

  # add your column key to the existing columns.
  $columns['mycolumn'] = __( 'Something different' ); 

  # now define a new order. you need to look up the column 
  # names in the HTML of the admin interface HTML of the table header. 
  #   "cb" is the "select all" checkbox.
  #   "title" is the title column.
  #   "date" is the date column.
  #   "icl_translations" comes from a plugin (in this case, WPML).
  # change the order of the names to change the order of the columns.
  $customOrder = array('cb', 'title', 'icl_translations', 'mycolumn', 'date');

  # return a new column array to wordpress.
  # order is the exactly like you set in $customOrder.
  foreach ($customOrder as $colname)
    $new[$colname] = $columns[$colname];    
  return $new;
}

hope this helps..

Method 3

the only way i know how is to create your own array of columns

// Add to admin_init function
add_filter('manage_edit-au-gallery_columns', 'add_my_gallery_columns');

function add_my_gallery_columns($gallery_columns) {
        $new_columns['cb'] = '<input type="checkbox" />';

        $new_columns['id'] = __('ID');
        $new_columns['title'] = _x('Gallery Name', 'column name');
                // your new column somewhere good in the middle
        $new_columns['thumbnail'] = __('Thumbnail');

        $new_columns['categories'] = __('Categories');
        $new_columns['tags'] = __('Tags');
        $new_columns['date'] = _x('Date', 'column name');

        return $new_columns;
    }

and then render this extra added columns like you normally would

// Add to admin_init function
    add_action('manage_au-gallery_posts_custom_column', 'manage_gallery_columns', 10, 2);

    function manage_gallery_columns($column_name, $id) {
        global $wpdb;
        switch ($column_name) {
        case 'id':
            echo $id;
                break;

        case 'Thumbnail':
            $thumbnail_id = get_post_meta( $id, '_thumbnail_id', true );
                // image from gallery
                $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
                if ($thumbnail_id)
                    $thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
                elseif ($attachments) {
                    foreach ( $attachments as $attachment_id => $attachment ) {
                        $thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
                    }
                }
                if ( isset($thumb) && $thumb ) {echo $thumb; } else {echo __('None');}
            break;
        default:
            break;
        } // end switch
}

Hope This Helps

Method 4

This is a combination of a few SO answers, hopefully it helps someone!

function array_insert( $array, $index, $insert ) {
    return array_slice( $array, 0, $index, true ) + $insert +
    array_slice( $array, $index, count( $array ) - $index, true);
}

add_filter( 'manage_resource_posts_columns' , function ( $columns ) {
    return array_insert( $columns, 2, [
        'image' => 'Featured Image'
    ] );
});

I found that array_splice() will not keep the custom keys like we need it to. array_insert() does.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment