Search code examples
wordpresscategoriesposts

Query Posts on Category template using the posts category


I have a custom post type called 'Case Studies' that I'm using for case studies and the default post type ('post') for blog posts. I have a custom taxonomy called 'product_categories' which is used for blog posts and case study posts.

On my category template 'taxonomy-product_categories.php' I want to separate 'blog' post excerpts from 'case study' post excerpts but I can't find a way to query the post archive?

I've tried is_post_type_archive(), is_archive(), is_category() and is_tax() but none of them filter the posts.

I've also tried querying the posts but it just loads all posts from that post type.


Solution

  • You should be able to distinguish post types using the post object in your while loop if you're using the standard like this:

        <?php while ($query->have_posts()) {
          $query->the_post(); ?>
    

    You could so something like this in your while loop:

        <div class="left-column">
           <?php
              if ($post->post_type === 'case-studies') {
    
                // Show your case study excerpts
    
              } else {
    
                   // No content found message
              }
    
           ?>
         </div>
    
         <div class="right-column">
           <?php
              if ($post->post_type === 'post') {
    
                // Show your post excerpts
    
              } else {
    
                   // No content found message
              }
    
           ?>
         </div>