0

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

  • 1
    Setting hide_empty to true will - as the name suggests - only return terms that are assigned to a post. – Jacob Peattie Apr 17 at 8:40
  • @JacobPeattie, okay I feel stupid - but what about only matching specific categories? – Shannon Hochkins Apr 17 at 8:48
  • Sorry, I’m not sure what you mean. Could you elaborate. – Jacob Peattie Apr 17 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 Apr 17 at 22:09
  • Hi Shannon Please Refer to below link https://wordpress.stackexchange.com/a/140738/164966 – Karan Naimish Trivedi Apr 18 at 10:08
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>";
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.