Custom post type: No posts found even if counter has a count

admin2025-04-19  0

I have a weird bug where I created a custom post type, started to create posts in it and when I went back on the WP-Admin page listing all of the posts of that custom post type, no posts are found. Even if the counter over the list lists 7 found posts.

I've got another custom post type in my site, with the exact same setup (except names), that works normally too.

Anybody encountered that bug before?

EDIT: I've found what was bugging, but now dont know how to fix it. Yes, in use a pre_get_posts to change an unrelated query, but it only acts on the post type pix_events and so it screws up every custom post type except events.

I guess i'm missing a return for default, but can't find what I am supposed to return in that case.

function pix_change_query( $query ) {
    if ( is_archive('pix_events') ) {
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'meta_key', 'date_work' );
        $query->set( 'meta_type', 'DATE' );
        $query->set( 'order', 'ASC' );
        $query->set( 'meta_query', array(
            array(
                'key'=>'date_work',
                'compare'=>'>=',
                'value'=>date('Ymd'),
                'type'=>'DATETIME',
            )
        ) );
    }

}
add_filter( 'pre_get_posts', 'pix_change_query' );

I have a weird bug where I created a custom post type, started to create posts in it and when I went back on the WP-Admin page listing all of the posts of that custom post type, no posts are found. Even if the counter over the list lists 7 found posts.

I've got another custom post type in my site, with the exact same setup (except names), that works normally too.

Anybody encountered that bug before?

EDIT: I've found what was bugging, but now dont know how to fix it. Yes, in use a pre_get_posts to change an unrelated query, but it only acts on the post type pix_events and so it screws up every custom post type except events.

I guess i'm missing a return for default, but can't find what I am supposed to return in that case.

function pix_change_query( $query ) {
    if ( is_archive('pix_events') ) {
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'meta_key', 'date_work' );
        $query->set( 'meta_type', 'DATE' );
        $query->set( 'order', 'ASC' );
        $query->set( 'meta_query', array(
            array(
                'key'=>'date_work',
                'compare'=>'>=',
                'value'=>date('Ymd'),
                'type'=>'DATETIME',
            )
        ) );
    }

}
add_filter( 'pre_get_posts', 'pix_change_query' );
Share Improve this question edited Nov 7, 2019 at 16:08 Fredy31 asked Nov 7, 2019 at 15:02 Fredy31Fredy31 8782 gold badges16 silver badges31 bronze badges 4
  • Welcome to WPSE. My guess is there is a filter in your code (theme or plugin) intended for the front-end that is also affecting the admin side. Did you recently add a some archive or filter code hooked to pre_get_posts for example? – jdm2112 Commented Nov 7, 2019 at 15:58
  • Thanks, you unbugged me, edited with the next problem i've got. – Fredy31 Commented Nov 7, 2019 at 16:09
  • Got it, my is_archive was too broad and applied it on all post type archives. The right condition to use was is_post_type_archive – Fredy31 Commented Nov 7, 2019 at 16:23
  • @Fredy31 Glad to hear you could solve it! Feel free to add your own answer and mark it as accepted, so future visitors will be able to benefit from it. (Might need to wait 1d or more so you actually can) – kero Commented Nov 7, 2019 at 16:25
Add a comment  | 

1 Answer 1

Reset to default 3

My pre get posts functions went sideways. My condition was applying for all archives, not just the events one. Here is the code that is right. Notice the is_post_type_archive.

function pix_change_query( $query ) {
   if ( is_post_type_archive('pix_events') && $query->is_main_query() ) {
       $query->set( 'orderby', 'meta_value_num' );
       $query->set( 'meta_key', 'date_work' );
       $query->set( 'meta_type', 'DATE' );
       $query->set( 'order', 'ASC' );
       $query->set( 'meta_query', array(
           array(
               'key'=>'date_work',
               'compare'=>'>=',
               'value'=>date('Ymd'),
               'type'=>'DATETIME',
           )
       ) );
       return;
   }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745021734a280421.html

最新回复(0)