admin管理员组文章数量:1023213
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>";
}
}
}
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-
1
Setting
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 - @JacobPeattie, okay I feel stupid - but what about only matching specific categories? – Shannon Hochkins Commented Apr 17, 2019 at 8:48
- Sorry, I’m not sure what you mean. Could you elaborate. – Jacob Peattie Commented Apr 17, 2019 at 10:52
- @JacobPeattie Let's say I have a taxonomy that's selectable because it's attached to a post - but that post (product) is in a category that isn't in the current category, so I don't want it to be selectable, does that make more sense? – Shannon Hochkins Commented Apr 17, 2019 at 22:09
- Hi Shannon Please Refer to below link https://wordpress.stackexchange/a/140738/164966 – Karan Naimish Trivedi Commented Apr 18, 2019 at 10:08
1 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>";
}
}
}
本文标签: pluginsOnly return taxonomies that are linked to a category amp product
版权声明:本文标题:plugins - Only return taxonomies that are linked to a category & product 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745576447a2157046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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