Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get_terms with a minimum # of posts WordPress

  • SOLVED

This one is really baffling me an I don't see anything in the codex. I want to basically return all terms in a taxonomy that have at least 5 posts- to weed out some one off or rarely use taxonomy terms. Any help is greatly appreciated.

I want to do this in the get_terms and not afterwards because I am using pagination. If I limit posts after get terms it would cause lopsided pagination - not to mention other possible adverse issues.

Answers (3)

2012-09-04

Francisco Javier Carazo Gil answers:

The idea would be:
1. Get an array of terms with less than minimum posts
2. Call get_terms() excluding this terms

Well I'm going to see how do this and I write you.


Francisco Javier Carazo Gil comments:

$terms = get_terms('your_taxonomy');
$excluded_terms = array();

foreach($terms as $term)
{
if($term->count < MINIMUM)
$excluded_terms[] = $terms->term_id;
}

get_terms('your_taxonomy', array('exclude' => $bad_terms));

This would be.


mackrider comments:

Brilliant Francisco!!!

Thanks a million.


Francisco Javier Carazo Gil comments:

Perfect,

If need more help tell me!


Francisco Javier Carazo Gil comments:

If this is finished, please vote me.

2012-09-04

Albert Shala answers:

Quick reply here and maybe a push in the right direction, you may need to use wp_query to hook onto the query before the query executes.
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );

More info here: [[LINK href="http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters"]]http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters[[/LINK]]

2012-09-04

Dbranes answers:

Hi mackrider

There is a way to modify the 'get_terms' function my using the 'term_clauses' filter.

If you want to change the term count to at least 5 you could to something like this:

function my_terms_clauses($clauses, $taxonomy, $args){
//print_r($clauses);
$clauses['where'] = str_replace("tt.count > 0","tt.count > 4", $clauses['where']);
return $clauses;
}
add_filter('terms_clauses', 'my_terms_clauses', 1);


You can also uncomment

//print_r($clauses);


to see explicitly how to modify it.

Hope this helps.