I'm trying to use my category.php file to display all posts of a certain custom post type (say "Company") with a given category. However, when I try to use it by navigating to domain.com/category/company/category1, which is the link automatically generated by wp_list_categories(), no posts appear.

The code I'm using is:

<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'type-company', get_post_format() ); ?>
<?php endwhile; ?>

What am I doing wrong?

3 Answers 3


I figured it out! For anyone having the same problem, I solved it by adding

$cat_id = get_query_var('cat'); 

right in front of the loop. Anyone having the same problem would probably also benefit from looking at this, too.

  • But this would not fetch child category posts? How to do that? Commented Feb 9, 2016 at 6:12

The correct way is to use pre_get_posts for that.

You can either add to show at the search.php and other pages from template hierarchy, like so:

add_action('pre_get_posts', function($query) {
    if ( ! is_admin() && $query->is_main_query() ) {

        if ( is_archive() || is_category() ) {
            $query->set( 'post_type', 'company' );

        if ( $query->is_search() ) {
            $query->set( 'post_type', array( 'company' ) );

To understand more about pre_get_posts() you should watch this talk of Andrew Nacin (one of the leader developers of WP)


Are you using the core category taxonomy, by applying it to your Custom Post Type, or have you defined a custom taxonomy?

If you're using a custom taxonomy, you need to use the template file taxonomy-{taxonomy}.php, or taxonomy-{taxonomy}-{term}.php.

But really, using category.php or taxonomy.php really may not be what you want to do. Have you tried using archive-{post-type}.php, for displaying the archive index list of posts from your Custom Post Type?

You also have an issue with this:

<?php get_template_part( 'type-company', get_post_format() ); ?>

The Post Format taxonomy only applies to the Post post-type; not to any, arbitrary post-type. Again: did you custom-apply the Post Format taxonomy to your Custom Post Type?


Note: you may need to modify the Loop query in category.php and/or tag.php, in order to display your CPTs.

  • I'm using the core category taxonomy for my Custom Post Type; the blog itself does not use categories. I could create an archive-{post-type}.php document, but I need to only display the posts in a given category, so category.php seems like a natural choice. Commented Jul 10, 2011 at 3:16
  • I've fixed the issue with get_post_format(): I'm now using get_template_part( 'content-company' ); and I've also tried type-company and single-company—none work. Commented Jul 10, 2011 at 3:17
  • Do you have template-part files named content-company.php, type-company.php, or single-company.php? Commented Jul 10, 2011 at 3:23
  • Yes, I do. Do I need to store them somewhere in particular? They're in the root of my theme folder. Commented Jul 10, 2011 at 3:30
  • Alternatively, do I need to use a special variant of the loop, or add a query before it? Commented Jul 10, 2011 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.