0

EDIT: I can't delete my question because of the open bounty, but it looks like the problem is caused by the "Post Types Order" plugin I have installed. I have it set to not order news post types, but for some reason it's still adding its sorting to the main query. It's my mistake, you should always disable plugins when debugging issues.

I'm trying to set the default sorting on a column for my custom post type news. The column sorts fine when the heading is clicked on, but when you first visit the post listing it will only sort by the default sorting (published date). Here's my pre_get_posts action:

add_action( 'pre_get_posts', function( $query ) {
    global $pagenow;

    if ( !is_admin() || $pagenow !== 'edit.php' )
        return;

    $post_type = $query->get( 'post_type' );
    $order_by = $query->get( 'orderby' );

    if ( $post_type == 'news' ) {
        if ( empty( $order_by ) )
            $order_by = 'news-date';

        switch ( $order_by ) {
            case 'news-date':
                $query->set( 'meta_key', 'date' );
                $query->set( 'orderby', 'meta_value_num' );
                break;
        }
    }
} );

After sorting manually by clicking the heading the URL becomes edit.php?post_type=news&orderby=news-date&order=asc so I'm sure all my parameters match up.

I tried adding a var_dump( $query ); in the switch statement after setting the query and the query includes the updated meta_key and orderby values, so I'm assuming the query is being modified correctly, but still it's doing the default sorting.

I thought maybe the date meta key was already used for some reason (maybe by a plugin) so I checked the wp_postmeta table in the database but the only meta keys named date were the values set with the ACF Date Picker, so everything is fine there.

Hopefully it's just something stupid I'm missing.

0
+50

it looks like the problem is caused by the "Post Types Order" plugin I have installed

Yes, I believe so because your code actually worked for me, but after I installed and activated the plugin, your code no longer worked as expected.

I have it set to not order news post types, but for some reason it's still adding its sorting to the main query.

I don't know how you set it, but try one of these:

  1. Set ignore_custom_sort (it's a custom WP_Query parameter) to true like so:

    switch ( $order_by ) {
        case 'news-date':
            $query->set( 'meta_key', 'date' );
            $query->set( 'orderby', 'meta_value_num' );
            $query->set( 'ignore_custom_sort', true );
            break;
    }
    
  2. Or you can also use the pto/posts_orderby/ignore hook to disable the plugin's sorting:

    add_filter( 'pto/posts_orderby/ignore', function( $ignore, $order_by, $query ){
        if ( 'lesson' === $query->get( 'post_type' ) ) {
            $ignore = true;
        }
        return $ignore;
    }, 10, 3 );
    
  • 1
    Thanks for the in-depth answer. I "fixed" my problem by just installing a different plugin (Simple Custom Post Order) but your answer is really good so I'll mark it as the answer and give you the bounty :) – Gavin Jun 29 '19 at 16:28
  • I actually had a thought of suggesting you to just switch to another plugin, but anyway, thanks a lot. :) – Sally CJ Jun 29 '19 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.