You’d probably do something like this
$args = array(
'post_type' => 'product',
'product_cat' => get_queried_object()
);
$query = new WP_Query( $args );
$termsInPosts = array();
if ( $query->have_posts() ) {
while( $query->have_posts() ) : $query->the_post();
$postTerms = wp_get_post_terms(get_the_ID(), 'brands', array('fields' => 'term_id'));
foreach ($postTerms as $postTerm)
if (!in_array($postTerm->term_id, $termsInPosts))
$termsInPosts[] = $postTerm->term_id;
endwhile;
}
$marcas_terms = get_terms([
'taxonomy' => 'brands',
'include' => $termsInPosts
]);
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>
}
if ( $query->have_posts() ) {
while( $query->have_posts() ) : $query->the_post();
get_template_part("templates/product-content-category");
endwhile;
}
wp_reset_postdata();
What you’re doing is just running through the loop before hand, getting the id’s of terms within the posts, adding to an array, then when you call get_terms()
you’re using include
to include only the terms you found.
I haven’t tested this code, it’s freehand, but it should work, or at least steer you in the right direction.
Hope that helps.