2

enter image description here

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' );
  • 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 Nov 7 at 15:58
  • Thanks, you unbugged me, edited with the next problem i've got. – Fredy31 Nov 7 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 Nov 7 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 Nov 7 at 16:25
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;
   }
}
  • 1
    If you want to only run this query in the admin panel you could return early with an if( is_admin() ) conditional. Or vise-versa. – Howdy_McGee Nov 7 at 16:55

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.