What's the best way, to return taxonomies, but only taxonomies that are linked to a product, within the category, I have a heirachial taxonomy, I want to get all the top level options, once an option is picked, I want to get all the children, but only the children that are linked to a product.
Is this possible?
Currently I'm just getting the immediate children of the first term picked, I don't know how to filter out the ones that have no matches.
This is the second request:
$terms = get_terms( 'tyre', array(
'orderby' => 'name',
'child_of' => $term_id,
'hide_empty' => $hide_empty
));
Is there a way to extend this to something like this:
$terms = get_terms( 'tyre', array(
'orderby' => 'name',
'category' => 'tyres',
'only_linked_to_products' => true,
'child_of' => $term_id,
'hide_empty' => $hide_empty
));
Obviously I've just invented these two properties, but it would help illustrate what I'm trying to do
What's the best way, to return taxonomies, but only taxonomies that are linked to a product, within the category, I have a heirachial taxonomy, I want to get all the top level options, once an option is picked, I want to get all the children, but only the children that are linked to a product.
Is this possible?
Currently I'm just getting the immediate children of the first term picked, I don't know how to filter out the ones that have no matches.
This is the second request:
$terms = get_terms( 'tyre', array(
'orderby' => 'name',
'child_of' => $term_id,
'hide_empty' => $hide_empty
));
Is there a way to extend this to something like this:
$terms = get_terms( 'tyre', array(
'orderby' => 'name',
'category' => 'tyres',
'only_linked_to_products' => true,
'child_of' => $term_id,
'hide_empty' => $hide_empty
));
Obviously I've just invented these two properties, but it would help illustrate what I'm trying to do
Share Improve this question asked Apr 17, 2019 at 8:39 Shannon HochkinsShannon Hochkins 1113 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 0This will find all terms in custom taxonomy underkategorier
and then will show terms with post from chosen category.
$taxonomies = get_taxonomies(array('name'=>'underkategorier'),'object');
foreach($taxonomies as $taxonomy) {
$terms = get_terms( $taxonomy->name, 'orderby=count&hide_empty=0' );
foreach($terms as $term) {
$wpq = array ('taxonomy'=>'underkategorier','term'=>$term->slug, "cat"=>10);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo $term->name.' '.$article_count; //with empty ones
if ($article_count){
echo "<ul>";
echo "<li>".$term->name.' '.$article_count."</li>";
echo "</ul>";
}
}
}
hide_empty
totrue
will - as the name suggests - only return terms that are assigned to a post. – Jacob Peattie Commented Apr 17, 2019 at 8:40