1
votes

I have 2 custom post type which is

a) support
b) item

and custom taxonomy called item_category

I have set this taxonomy to those 2 custom post.

Now, I want to count the post from support custom post based on added taxonomy term.

For example:

Taxonomy item_category has 3 terms : 1) blog 2) lms 3) event

I have added this term blog to support custom post.

Now, I want to get number of post from only support post type under blog term

To do that I am using

$item_categories = get_terms(array(
    'taxonomy'   => 'item_category',
    'hide_empty' => false,
));

echo "<ul class='card-content'>";
foreach ($item_categories as $item_category) {

    echo '<pre>';
        print_r($item_category);
    echo '</pre>';

    $item_category_id   = $item_category->term_id;
    $item_category_name = $item_category->name;
    $item_category_slug = $item_category->slug;
    $item_category_count = $item_category->count;

    echo "<li><a href=".site_url("/support/?item_category={$item_category_slug}").">";
        echo "<span class='lnr lnr-chevron-right'></span>{$item_category_name}";
        echo "<span class='item-count'>{$item_category_count}</span>";
    echo "</a></li>";
}
echo "</ul>";

But its counting from both support and item custom post. I want only from support post.

Currently, the output is like that with count value but the count is wrong according to my need.

enter image description here

is it possible?

1
i am not sure it is possible or not but i think this might be helpful : wordpress.stackexchange.com/questions/57444/… - Darsh khakhkhar

1 Answers

0
votes

You can get posts related to taxonomy with a tax query:

$posts_array = get_posts(
    array(
        'posts_per_page' => -1,
        'post_type' => 'support',
        'tax_query' => array(
            array(
                'taxonomy' => 'item_category',
                'field'    => 'term_id',
                'terms'    => $item_category_id,
            )
        )
    )
);

echo count($posts_array);