Display page of custom posts?

I think your problem is in the exact code that you’ve mentioned. pre_get_post is a bastard if you don’t use it correctly.

With any archive type conditions used in pre_get_posts, it will affect both front end and back end. This include archives pages, category and taxonomy pages, tag pages and author pages. You’ll need to do a check that you only run pre_get_posts on the front end (use !is_admin()).

One other important thing you’ll need to do is to run pre_get_posts only on the main query. Because pre_get_posts runs first before the main query and WP_Query and modify the query variables, both the main query and any custom queries using WP_Query will be affected. (Use is_main_query())

So you will need to modify the code in question to this to add the !is_admin check

function namespace_add_custom_types( $query ) {
    if( !is_admin() && $query->is_category() && $query->is_main_query ) {
        $query->set( 'post_type', array(
            'post', 'your-custom-post-type-here'
));
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

I hope this helps. If not, let me know.

EDIT

Steve, before you make these changes I’ve proposed, first check my answer on your other question. But please keep the points in mind in this answer for future reference.

Leave a Comment