Working on a category archive, i have a sidebar with custom non hierarchical taxonomies, used as filters (like custom post_tags).

In order to list those filters, I use wp_get_object_terms:

global $wp_query;
$posts = array();

foreach( $wp_query->posts as $post ){
    $posts[] = $post->ID;

$matieres = wp_get_object_terms($posts, 'matieres');

if( count($matieres)<1 ){
    <div>No filterable post</div>
} else {
    <div class="accordion">
        <div class="accordion-item">
                <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseMatieres" aria-expanded="true" aria-controls="collapseMatieres">
            <div id="collapseMatieres" class="accordion-collapse collapse show">
                <div class="accordion-body">
            foreach( $matieres as $matiere ){
             <div class="form-check">
              <input class="form-check-input filter_check" type="checkbox" data-filtre="matieres" value="<?php echo $matiere->slug; ?>">
              <label class="form-check-label" for="flexCheckChecked">
                <?php echo $matiere->name . ' (' . $matiere->count . ')'; ?>

Everything works fine, but for each custom taxonomy, wordpress counts me ALL posts in all categories, and not only posts within the current category.

In picture below, you can see it in action : in this category, i have only 6 inox products, but wp_get_object_terms counts me the 88 posts website wide.

enter image description here

Question is : what args should i pass with wp_get_object_terms to count only current category posts ?

  • Where are you getting the counts from? I don't see any code in your question that would display what is in your screenshot, and no code for retrieving and displaying the counts. Can you edit your question to include this?
    – Tom J Nowell
    Commented Sep 22, 2022 at 17:02
  • I get the count from the returned terms of wp_get_object_terms.
    – rafa226
    Commented Sep 22, 2022 at 17:27
  • It looks like it works fine for most of the "tags" and fails only on Inox, is that right? Anything special about that one? Commented Sep 22, 2022 at 18:47
  • 1
    WordPress doesn't have functions that support what you're trying to do, and the database is not well optimised for it. If you want to get the count of products that are in Inox and the current category you'll need to use an SQL query to count posts in both categories. Commented Sep 22, 2022 at 20:59
  • @NabhaCosley : nope, all terms are impacted
    – rafa226
    Commented Sep 23, 2022 at 7:26

1 Answer 1


A simple function to count posts did the trick, but this is one more request in DB :

function count_posts_in_cat_by_custom_term( $category_ID, $custom_taxonomy_name, $custom_term_id ){
    $args = array(
        'post_type'     => 'post',
        'post_status'   => 'publish',
        'posts_per_page' => -1,
        'category' => $category_ID,
        'tax_query' => [
                'taxonomy' => $custom_taxonomy_name,
                'terms' => $custom_term_id,
    $posts = get_posts($args);
    return count( $posts );

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.