I am stuck trying to get a custom column to sort according to a custom field. I have two custom post types: Brand and Product. I would like the Product admin screen to have a "Brand" column, and for that "Brand" column to be sortable by the custom field _wpcf_belongs_brand-listing_id.

I was able to successfully add a new "Brand" column to the "Product" admin screen

I then successfully registered the Brand column as sortable. The "Brand" custom column has a key of brand and a value of _wpcf_belongs_brand-listing_id, which is the custom field I want to sort by.

//Register the Brand column in the Product admin page as sortable.    
add_filter( 'manage_edit-eproduct_sortable_columns', 'vendia_sortable_brand_column');

function vendia_sortable_brand_column( $sortable_columns ){
    $sortable_columns[ 'brand' ] = '_wpcf_belongs_brand-listing_id';
    return $sortable_columns;

To confirm the above function worked correctly, I use a debug() function that sends the output of print_r() to Firebug's console.
The debug() function output is:

        [title] => title
        [parent] => parent
        [comments] => comment_count
        [date] => Array
                [0] => date
                [1] => 1
        [brand] => _wpcf_belongs_brand-listing_id

So I know that the "Brand" custom column is registered correctly.

However, my "Brand" custom column sort function is not working. I would like to sort this column by the custom field _wpcf_belongs_brand-listing_id.

//Tell WordPress what to sort by in the Brand column of the Product admin page.
add_action( 'pre_get_posts', 'vendia_brand_column_orderby' );
function vendia_brand_column_orderby( $query ) {

    if( ! is_admin() )

    $orderby = $query->get( 'orderby' );
    echo $orderby;

    if( '_wpcf_belongs_brand-listing_id' == $orderby ) {


For some reason I don't understand, my debugging statement of echo $orderby; returns a value of date for the key orderby for every sortable column. I was the expecting orderby parameter to take on the values of "title", "parent", "comment_count", "date", and "_wpcf_belong_brand-listing_id". Those are the values associated with each custom column key, output by my debugging function above.

According to http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

date is simply the default value for the orderby parameter of WP_Query. So it seems like the WP_Query has not been changed from its defaults at this point, meaning, at the point that pre_get_posts fires. However, every example I have seen that discusses adding a custom column, and making it sortable, uses pre_get_posts for changing the orderby parameter for the custom columns.

So am I using the wrong WordPress action (pre_get_posts) to change the orderby parameter for custom columns ? If not, why is my WP_Query returning date as the orderby parameter for every custom column?

Thank you for your help. I know this was a really long question.

  • Try checking for $query->is_main_query() also - eg if( ! is_admin() || ! $query->is_main_query() ) return; ...
    – bonger
    Oct 5, 2015 at 12:27
  • 1
    That did it @bonger. Thanks so much. Struggled with this for days. Wish you made this an answer so I could upvote it. Oct 5, 2015 at 17:42


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.