2

Is there an argument for get_terms which I can fetch terms that only have say over 2 posts associated with it?
I have a terms page which lists all my terms for 'artists', the page is huge but a lot of these terms that only have one post so I would like to show only significant terms.

2 Answers 2

2

Give:

$terms = get_terms("my_taxonomy");
$count = count($terms);
if ( $count > 0 ){
    echo "<ul>";
    foreach ( $terms as $term ) {
        if ($term->count > 2) {
            echo "<li>" . $term->name . "</li>";
        }
    }
    echo "</ul>";
}

a shot. It will grab all the terms and then run a check to see if the $term->count is greater than 2 and if so, print out those terms.

2

This does basically the same as @Zach already added, but in a more smart/unreadable way :)

$taxons = get_terms(
     'some_taxonomy'
    ,array(
         'hide_empty' => true // is the default
     )
);
$count = count( $taxons );
$stack = array()
if ( 0 < $count)
{
    // Catch all terms that have a count of "1"
    // As we already have excluded all with 
    // a zero count are already excluded
    $to_exclude = wp_list_filter(
         $taxons
        ,array( 'count' => 1 )
        ,'AND'
    );

    // fill our stack by filtering/diffing our 1-post taxons out
    $stack = array_diff( (array) $taxons, (array) $to_exclude )
}

echo '<pre>'.var_export( $stack, true ).'</pre>';
7
  • You and your damn crafty answers :)
    – Zach
    Oct 12, 2012 at 19:52
  • @Zach Glad you answered - remembered me to upvote yours for "most readable" ;) - Btw: I haven't measured it, but I think with an iteration of 10k rounds involving timer_start()/*_stop(), yours will even be faster.
    – kaiser
    Oct 12, 2012 at 19:59
  • Ah nice, always good to know
    – Zach
    Oct 12, 2012 at 20:02
  • Hm I'm getting this error: Catchable fatal error: Object of class stdClass could not be converted to string
    – Imran
    Oct 12, 2012 at 21:49
  • @Imran I'm pretty sure the problem is the objects inside the array, but please try it again and report back. In case, I'll rework it with an array_map another time.
    – kaiser
    Oct 12, 2012 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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