Custom Post Type Dashboard Tab not displaying any posts

I created a custom post:

/*= Add Custom Post Type: Board Members
/****************************************************/ 
add_action( 'init', 'create_board_posttype' );
function create_board_posttype() {
  register_post_type( 'board',
    array(
      'labels' = array(
        'name' = __( 'Board Members' ),
        'singular_name' = __( 'Board Member' ),
        'add_new_item' = __( 'Add New Board Member' ),
        'edit_item' = __( 'Edit Board Member' ),
        'new_item' = __( 'New Board Member' ),
        'view_item' = __( 'View Board Member' ),
        'search_items' = __( 'Search Board Members' ),
        'not_found' = __( 'No Board Members Found' ),
        'archives' = __( 'Board Members Archives' ),
        'insert_into_item' = __( 'Insert into Board Member' ),
      ),
      'public' = true,
      'supports' = array( 'title', 'editor', 'custom-fields', 'thumbnail', 'page-attributes' ),
      'has_archive' = true,
      'rewrite' = array('slug' = 'board'),
    )
  );
}

I then went and added 10 posts successfully and they are displaying on the frontend fine.

Fast forward 4 weeks and I went in to edit a post but I am seeing No board members found.

It shows that there are 10 Board Members published by actually displays none in the list:

All (10) | Published (10)

No Board Members Found

Clearly WP knows there are board members (All (10)) but it wont show them... I don't know where to start?

Ok, I found the issue but I don't understand why.

I have another custom_post_type: jobs

On the frontend I try to filter jobs within last x days. When I remove this code, the Board Members re-appear:

add_action( 'pre_get_posts', 'custom_query_vars' );
function custom_query_vars( $query ) {
    
    if ( $query-is_main_query() ) 
    {
        if ( is_archive('job') ) 
        {
            $days = get_field('job_expiry_days', 'option');
            $query-set( 'date_query', array(
                                'column'  = 'post_date',
                                'after'   = '- '.$days.' days'
            ) );
        }
    }
    return $query;
}

Topic functions init custom-post-types Wordpress

Category Web


This may not answer the question, but note that is_archive() does not accept any parameters. If you want to check if the query is for an archive of a custom post type, use is_post_type_archive(), e.g. is_post_type_archive( 'job' ) if the post type is job.

And for example, if the CPT archive was at example.com/jobs, then if ( is_post_type_archive( 'job' ) ) would return true only when viewing that /jobs page.


pre_get_posts doesn't just run on the frontend, it runs on all post queries, and that includes RSS feeds, XMLRPC, AJAX, REST API, and WP Admin.

You need to check in your pre_get_posts filter and exit early so that it only applies to the places you want it to apply to.

e.g.

if ( is_admin() ) {
    return;
}

Additionally, this code is incorrect:

if ( is_archive('job') ) 

This checks if the main query is a job archive, but it should be checking if $query is a job archive, so it needs to be $query->is_archive( 'job' ).

This is also followed by another bug:

$days = get_field('job_expiry_days', 'option');

If job_expiry_days is not set, then it will be a falsey value, resolving to 0, meaning only posts older than 0 days ago will be found, aka nothing.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.