Unable to display posts for one CPT category

Question

I have created a custom post type, and added categories to it. I am wanting to display the posts inside only one of these categories and am struggling to see why my code isn’t working. (If I remove the array specifying the category the query returns all posts inside the CPT, so it’s working fine that way.)

Query:

$args = array(
    'post_type' => 'research',
    'order' => 'ASC',
    'posts_per_page' => 9,
    'category_name' => 'cereals', // <-- I want to display this category
    'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
    );  

$the_query = new WP_Query( $args ); ?>

Display:

<?php if( $the_query->have_posts() ) : ?>
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

      <h2><?php the_title(); ?>

  <?php endwhile; ?>
<?php endif; ?>

I have had a look at various other questions on here and StackOverflow but cannot seem to find an answer to my issue. I’ve also tried cat to specify the ID of the category, with no luck.

0
, , MattHamer5 2 years 2020-01-06T08:39:07-05:00 0 Answers 83 views 0

Leave an answer

Browse
Browse