custom taxonomy – wp_get_object_terms count on taxonomies within an category archive

Question

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:

<?php
global $wp_query;
$posts = array();

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

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

$filters = array(
    "matieres" => $matieres,
);

if( count($matieres)<1 ){
    ?>
    <div>No filterable post</div>
    <?php
} else {
?>
    <div class="accordion">
        <div class="accordion-item">
            <h5>
                <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseMatieres" aria-expanded="true" aria-controls="collapseMatieres">
                    Matières
                </button>    
            </h5>
            <div id="collapseMatieres" class="accordion-collapse collapse show">
                <div class="accordion-body">
            <?php
            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 . ')'; ?>
              </label>
            </div>           
            <?php
            }
            ?>
                </div> 
            </div> 
        </div>        
    </div> 
<?php
}
?>

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 ?

0
rafa226 2 days 2022-09-22T11:40:17-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse