I have an entry with the following category and subcategory

----papaya (Main)

The problem is that when I show the related articles, it shows me all the entries of the category (crops) and not of the subcategory that you select as main (papaya)

this is the code i have:

$terms = get_the_terms( get_the_ID(), 'post_tag');

$args = array (
'category__not_in'           => $tags,
    'post__not_in' => array(get_the_ID()),
    'posts_per_page'         => '10',
    'ignore_sticky_posts' => 1,
    'meta_key' => '_thumbnail_id',

$query = new WP_Query($args);

if ($query->have_posts()) { ?>
    <section class="entry-related">
        <h3>Articulos relacionados</h3>
        <div class="flex flex-fluid">
            while ($query->have_posts()) {
                get_template_part('template-parts/loops/loop', 'related');


How would you show me only the entries in the subcategory that I select as the main one?

Thank you for your help

1 Answer 1


There are a couple of problems with that code

Selecting the wrong terms

First of all, you are storing tags instead of categories in the $terms variable. The taxonomy name for categories is category. That line should look like this.

$categories = get_the_terms( get_the_ID(), 'category');

For sake of expresivity, I changed the $terms variable to $categories.

Wrong query parameter for categories

You are using category__not_in, telling the query NOT to search for posts with those caregories. It is not only returning posts from crops, it returning every post that doesn't has the current category.

In order to tell the query that you want posts from a category or group of categories, you should use category__in

You can see more about accepted args for the WP_Query here

Wrong value for category__in

The category__in parameter for the query takes as value either a string or an array of categories ids (strings). The items stored in the $categories array are instances of WP_Term.

In order to pass the right value, you should create an array of ids from the array of WP_Term. We can get a WP_Term id by accesing the term_id property.

'category__in'           => array_map(function($cat){ return $cat->term_id; }, $categories)

array_map returns every item from the array after aplying a callback function to each of them. In this case, we are making an array of terms_ids from the $categories array.

Final Result

This is how your query should look like

$categories = get_the_terms( get_the_ID(), 'category');

$args = array (
    'category__in'                  => array_map(function($cat){ return $cat->term_id; }, $categories),
    'post__not_in'                  => array(get_the_ID()),
    'posts_per_page'                => '10',
    'ignore_sticky_posts'           => 1,
    'meta_key'                      => '_thumbnail_id',

$query = new WP_Query($args);

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.