Working on revamping a plugin of mine when I noticed that wp_count_terms() is not reflecting the same number as a call to count( get_terms() ) with the same $args passed. I'm pretty sure this is a bug in WordPress and I'll submit a trac ticket in a little bit.

I wanted to get a solid working solution, so I ended up just making one call to get_terms() and then using count() on the array to get the count. My question is: "How many terms can be returned without slowing everything down". It's possible that there could be scaling issues here where there are a lot of terms in a taxonomy, but how many is TOO many? 1,000? 10,000? Just looking for a ballpark here.

  • 1
    This got anywhere since? wp_count_terms() is essentially thin wrapper on top of get_terms() so it seems strange. Code examples? – Rarst Jul 18 '11 at 8:17
  • No better solution yet. May need to put in a ticket for 3.3. At least in 3.1, wp_count_terms() will force a few parameters which throws the counts off for certain custom queries like the one I was using. It works well for the core queries though. – mfields Jul 19 '11 at 23:44

Ad slowing down)

Lame answer: depends on your server and stuff.

Ad possible bug)

wp_count_terms(); is a level "above" get_terms(); and therefore has values like 'hide_empty' and 'fields' already set. I'd say: diff your $args against those predefinied by wp_count_term();. The later function does nothing than calling the get_terms() at it's end.

  • The "Lame answer" is what I'm looking for. Could you please expand on this? I'm aware that wp_count_terms() calls get_terms(). There is no diff between the args passed to both functions. With the "fields" argument set to "count" get_terms() will build an optimized query that (I believe) will be faster/less-memory-intense than a direct call to get_terms() – mfields May 18 '11 at 21:08
  • I'm not good at server related stuff, so this answer will stay "lame". Sry :) – kaiser May 18 '11 at 21:48
  • @mfields A starting point would be to do a manual sql query like echo $GLOBALS['wpdb']->query( 'EXPLAIN SELECT * FROM '.$GLOBALS['wpdb']->prefix.'terms' ); to see what the table offers. Then maybe inspect the indices and add some if this would benefit from it. If you got a MyISAM DB table (and not InnoDB), then maybe you could do something like PACK_KEYS=1 to speed it up, as reading is what the terms are mostly used on the viewer facing side. – kaiser Aug 15 '11 at 1:55
  • Please post a code sample. I think @kaiser is on to something with the hide_empty param. – brandwaffle Dec 23 '11 at 15:47

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.