0

I am developing a custom theme with custom post types involved. Custom post types have been defined via plugin 'Pods' (http://pods.io).

When writing a category.php, I realized, that the standard loop does not retrieve custom post types (CPT) posts which belong to a certain category. Is that right? If not, are there any template tags available to render CPT posts of a specific category?

Or is the right way to rather retrieve CPT posts via WP_Query?

1

Custom post types are by default excluded from the main query except on taxonomy pages and custom post type archives.

You can simply use pre_get_posts to correctly alter the main query (alter the query variables before the SQL query is build and executed) to your needs.

Just a few notes on pre_get_posts

  • pre_get_posts runs front end and back end queries, so it is very important to do the is_admin() check for queries only meant to run front end or back end

  • pre_get_posts alters all custom instances of WP_Query, get_posts() (which uses WP_Query) and the main query (which also uses WP_Query). You would want to use the is_main_query() check to specifically only alter the main query.

You can do the following in a plugin or your theme's functions.php

add_action( 'pre_get_posts', function ( $q )
{
    if (    !is_admin() // Only target front end queries
          && $q->is_main_query() // Only target the main query
         && $q->is_category() // Only target category archives
    ) {
        $q->set( 'post_type', ['post', 'custom_post_type'] ); // Adjust as needed
    }
)};
  • That did the trick, fantastic! Thnx – Bunjip Jan 19 '16 at 19:50
  • Glad it worked. Enjoy ;-) – Pieter Goosen Jan 20 '16 at 9:59
0

You need to use taxonomy.php instead of category.php

Give little reading to template heirarchy on https://developer.wordpress.org/themes/basics/template-hierarchy/

All your custom posts will use taxonomy.php if you have some custom taxonomy defined.

For eg: lets suppose there are two taxonomies i.e tax1 & tax2

If you want the same template for both taxonomies then go with taxonomy.php

But if you want different templates for tax1 & tax2 then use

taxonomy-tax1.php & taxonomy-tax2.php accordingly

I hope i was able to clear out few things for you .

All the best

  • Thanks for your answer, @terminator. I'm going to try taxonomy,php as you suggested. I haven't used it so far, because I haven't actually defined a custom category taxonomy but rather used the default 'category' taxonomy with my custom post type. Hence, I was assuming that any standard category.php (or other archive.php) template file would also fetch CPT posts by default... – Bunjip Jan 13 '16 at 8:31
  • actuallly standard archive.php would do the job but not the category.php – terminator Jan 13 '16 at 10:12
  • If I use standard archive.php, Wordpress routing does not consider which category has been called. Say, if user requests example.com/category/my-category archive.php does not know the category id to filter cpt posts from. I guess, I'll have to get into hooks to alter the default query behaviour of the category.php template...? – Bunjip Jan 13 '16 at 10:49

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.