Iâve hit another small rock in my current project!
Iâm trying to list (and link) to the archive page for the top 4 most used terms for a custom post type. I have this;
get_terms('trade',array('orderby'=>'count','order'=>'DESC','number'=>4))
..which is along the right lines but I canât seem to find a way to pin this down to a specific post type. There is one taxonomy called âtradeâ across 6 custom post types on the website, I need to find out the top 4 for each one. I started looking through the tables and found I could probably query by the post type, and order by the âcountâ column in the wp_term_taxonomy table. However, that is really pushing my limits with MySQL because I donât know an efficient way to write what seems to be such a complex query.
Iâve been on this for 4hrs or so now, I donât think the penny is going to down without some help!
Thanks for taking a look…
I if were you, I’d have two options in my mind. Both hinge on the fact thsi is an inherently expensive computation and no mysql trickery will reduce the load with the data you have.
So
Option 1, do it the expensive way and store the data for a rainy day
You’re going to have to swallow the cost of calculating this, but you don’t have to do it over and over again. Calculate it the long expensive way and save the post IDs in a transient with a long expiration time. You’ll have a one time hit followed by it running super fast until the transient runs out.
For bonus points, do the calculation in a wp-cron task once a day and set the expiration time to two days,
Option 2, create helper data
You have 1 taxonomy and 6 post types occupying it. To get around this, create another taxonomy, a hidden taxonomy with no GUI. This taxonomy will only be attached to your desired post type.
Then, hook into the term creation/deletion/assignment hooks, and use them to duplicate the taxonomy data but only for that post type. That way when you want to do your list, you query the second hidden taxonomy, which acts as a cache of sorts. All the hard work of figuring out which term has how many is done as you make the changes
Just because you don’t know how to handle the data you have performantly, doesn’t mean there is a solution, but changing the data made available to you can sometimes make a world of difference, and that doesn’t always mean adding more cache.
Here’s a crude example on cloning a taxonomy:
Place a second check after checking the taxonomy to figure out which post type it is and if the post type matches, clone the term, else don’t.
So, after much time spent on this I seem to have found a method that works for me. I took Tomâs cloning idea and his advice and made a second taxonomy that sits our of view but can still be queried.
This is me registering the unique taxonomies in my plugin file using a loop linked to an array with various bits of data;
Once thatâs made I added this is my functions.php file so I could add the additional data to the new taxonomies by copying (or cloning as Tom Suggested) the terms from the main taxonomy. There is probably a more efficient way of writing some of this, but this seems to be as far as my PHP goes right now…
This works well for me as the terms are automatically written AND updated with any custom post publish or revision.
Finally I can query my top 4 terms and link to the archive pages;
Perhaps I could do this a better way, but whatever – it works 😀
Many thanks to Tom for his repeated input.