Get taxonomy terms only of the WP_Query current posts

I’m trying to find a way to not show all the terms of a taxonomy, but only the terms of the current displayed posts of the wp_query.

The code below displays all terms for ‘brands’ taxonomy and the query displays all products of the current category, i want to display only the terms that are set on the products that are displaying.

$marcas_terms = get_terms([
‘taxonomy’ => ‘brands’
]);

foreach ($brands_terms as $brand_term) {
<input type=”checkbox” id=”<?php echo $brand_term->term_id; ?>” name=”marca” value=”<?php echo $brand_term->term_id; ?>”>
<label for=”<?php echo $brand_term->term_id; ?>”><?php echo $brand_term->name; ?></label>
}

$args = array(
‘post_type’ => ‘product’,
‘product_cat’ => get_queried_object()
);

$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while( $query->have_posts() ) : $query->the_post();

get_template_part(“templates/product-content-category”);

endwhile;
} wp_reset_postdata();

Read more here:: Get taxonomy terms only of the WP_Query current posts

Leave a Reply

Your email address will not be published. Required fields are marked *