1

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.

1

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'
  • I didn't register any custom taxonomies. I only created some categories in the admin panel. The problem is using category__not_in works as expected (returns all custom posts without my category assigned to). Using the opposite: cat or category_name always return empty lists. Is my thinking good here that switching between cat and category__not_in should show me the opposite results? – bdobry Oct 18 '17 at 11:02
  • That's correct thinking. Remember cat is for ID and category_name is the name Not the slug. – rudtek Oct 18 '17 at 13:21
0

Have you tried using something like this in the array, Had a similar problem earlier which this solved.

'taxonomy' => 'your_taxonomy_name',

Or something like :

    'tax_query' => array(
        'taxonomy' => 'your_taxonomy_name',
        'terms' => 'Alumni',
        'field' => 'slug',
        'include_children' => true,
),
  • taxonomy returns all of my custom post types. I quickly played around with tax_query settings you proposed but I always got empty list :( edit: also, just a quick note. In the last example there shouldn't be category__not_in. Maybe someone coming here in the future won't get confused :) – bdobry Oct 17 '17 at 20:42
0

To get custom post type posts with specific category use custom taxonomy

Register the taxonomy name of the custom post type like categories and then assign a category to each post when you added new post. Here is the example of the code

  add_action( 'init', 'news_my_taxonomy');
  function news_my_taxonomy(){
 // custom post type taxonomies
    $labels = array(
    'name' => 'Categories',
    'singular_name' => 'Category',
    'add_new' => 'Add Category',
    'add_new_item' => 'Add New Category',
    'all_items' => 'All Categories',
    'edit_item' => 'Edit Item',
    'new_item' => 'New Item',
    'view_item' => 'View Item',
    'update_item' => 'Update Category',
    'search_items' => 'Search Categories',
    'not_found' => 'No record found',
    'not_found_in_trash' => 'No items found in trash',
    'parent_item_colon' => 'Parent Item',
    'menu_name' => 'Categories'
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'news_category'),
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        );
        register_taxonomy('news_category', array('news'), $args);
}

then

create taxonomy template page 'taxonomy-news_category.php'

and add the query to get the posts with this category name

   $cat_name = single_cat_title;
   $args = array( 'category_name' => $cat_name, 'posts_per_page' => 12, 'order'=> 'ASC', 'post_type' => 'news', 'paged' => $paged);

All the work is done.Good luck

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.