最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - How to show entries related to main category

programmeradmin4浏览0评论

I have an entry with the following category and subcategory

-Crops
---fruit
----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">
            <?php
            while ($query->have_posts()) {
                $query->the_post();
                get_template_part('template-parts/loops/loop', 'related');
            }
            ?>
        </div>
    </section>

    <?php
}
wp_reset_postdata();

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

Thank you for your help

I have an entry with the following category and subcategory

-Crops
---fruit
----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">
            <?php
            while ($query->have_posts()) {
                $query->the_post();
                get_template_part('template-parts/loops/loop', 'related');
            }
            ?>
        </div>
    </section>

    <?php
}
wp_reset_postdata();

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

Thank you for your help

Share Improve this question edited Dec 16, 2019 at 3:13 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Dec 15, 2019 at 18:35 Plantas y remedios CaserosPlantas y remedios Caseros 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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);
发布评论

评论列表(0)

  1. 暂无评论