get_terms with more than x post count

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.

Related posts

Leave a Reply

2 comments

  1. 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>';