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
  • 1 Setting hide_empty to true 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
 |  Show 1 more comment

1 Answer 1

Reset to default 0

This 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 to true 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
 |  Show 1 more comment

1 Answer 1

Reset to default 0

This 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