I try to query custom post type called news by a category called Alumni with ID=160.

When I use such arguments, as a result, I get all my custom posts without Alumni category:

$args = array(
  'posts_per_page' => -1,
  'post_type' => 'news',
  'orderby' => 'date',
  'order' => 'DESC',
  'category__not_in' => 160
);
$loop = new WP_Query( $args );

<?php while ( $loop->have_posts() ) : $loop->the_post();?>
...
<?php endwhile; ?>

However, changing category__not_in to category__in gives an empty list but I would expect the opposite of the initial result. I can’t really understand where I make a mistake.

Also, I tried using cat and category_name instead and I played around with different categories but results were always the same.

In my research I came across 'tax_query' but I can’t get it to work as well. The documentation is not quite clear for me.

3 Answers
3

Are these custom taxonomies or the regular categories?

if they are just categories you should use:

$args = array(
  'posts_per_page' => -1,
  'post_type' => 'news',
  'orderby' => 'date',
  'order' => 'DESC',
  'category_name' => 'Alumni'
);
$loop = new WP_Query( $args );

<?php while ( $loop->have_posts() ) : $loop->the_post();?>
...
<?php endwhile; ?>

if you want to use it by id

use:

'cat' => 160 

instead of

'category_name' => 'Alumni'

Leave a Reply

Your email address will not be published. Required fields are marked *