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.
Add a comment
|
2 Answers
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.
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>';
-
-
@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.– kaiserCommented Oct 12, 2012 at 19:59 -
-
Hm I'm getting this error: Catchable fatal error: Object of class stdClass could not be converted to string– ImranCommented 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.– kaiserCommented Oct 12, 2012 at 22:03