Im currently building filters for my archive pages. Since I'm working on an ecommerce project (woocommerce) I using some additional custom terms like brands. Considering a user is on the product category archive T-Shirts, I now want to display all brands in the archive page which are related to T-Shirts.
With get_terms
I can only display all brands, but not "cross-filtered" by T-Shirts. Any help is well appreciated. I also might be totally wrong with my approach.
My approach until now: (This cant be a common way)
$product_ids = get_posts(array(
'numberposts' => -1, // get all posts.
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term_id,
),
),
'fields' => 'ids', // Only get post IDs
));
foreach ($product_ids as $test_id) {
$aaa = wp_get_post_terms($test_id, 'manufacturer', array('fields' => 'ids'));
$brands[] = $aaa[0];
}
$brands = array_unique($brands);
I'm having trouble figuring out what "brands" is. Your wp_get_post_terms statement is going to get all the posts for each category in taxonomy "manufacturer." If manufacturer = brands, you can add this loop to cycle through them:
foreach ($product_ids as $test_id) {
$post = get_post($test_id);
echo '>> ' . $post->post_name . '</br>';
$aaa = wp_get_post_terms($test_id, 'manufacturer',
array('fields' => 'id=>slug'));
foreach( $aaa as $a) {
echo $a . '</br>';
}
}
You can then add 'if' statements to make your comparisons.