Let’s say I have a website which sells cars.
I’m using a custom taxonomy called brand
for the manufacturer (like BMW, Audi, …) and a custom taxonomy called type
for the type of cars (like SUV, Coupe, …).
For the cars itself I’m using a custom post type called models
.
Now I want to show every car brand
in the taxonomy archive for type
(All brands with SUVs).
To do that, I’m trying to get all brands
and filter them with all types
.
As result there should be a list with all car brands that have SUVs.
Here’s my current code to get a list of brands:
$taxonomies = get_terms( array(
'taxonomy' => 'brand',
'hide_empty' => false
) );
if ( !empty($taxonomies) ) :
$output = '<select>';
foreach( $taxonomies as $category ) {
if( $category->parent == 0 ) {
$output.= '<optgroup label="'. esc_attr( $category->name ) .'"></optgroup>';
}
}
$output.='</select>';
echo $output;
endif;
I couldn’t find a way to add a second taxonomy to this snippet.
Is this the wrong approach?
Maybe I need to get the custom post types (models) first to check which one has both terms?
2
Answers
I found a solution that works:
This article has a really helpful breakdown.
I needed to get the taxonomies that were part of another taxonomy for use in creating a dropdown (and some other uses.) This code allowed me to identify the "categories" that were part of a particular "destination".