最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Changing default admin column sorting to an ACF Date Picker field

programmeradmin1浏览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.

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.

Share Improve this question edited Jun 27, 2019 at 17:53 Gavin asked Jun 25, 2019 at 16:08 GavinGavin 4347 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 );
    
发布评论

评论列表(0)

  1. 暂无评论