How to organize and cache additional data associated with terms?

I’m updating a plugin of mine and am have a hard time figuring out the “best way” to build in a specific bit of functionality. The plugin allows users to associate images from their media library to any term of any taxonomy. I’m currently working on creating functions that users can add to their themes and plugins to get the data stored by the plugin. The code I have so far can be viewed here: https://github.com/mfields/Taxonomy-Images/blob/master/public-filters.php

The functionality presented in this file enables users to get the image of the term currently being queried (for taxonomy archive views) and another function is basically a wrapper for core function get_terms() which will return all terms of a given taxonomy while adding the image id to each term object. I would like to add a third function to the file before release. This would be a wrapper for get_the_terms() and would return all terms associated with the global post object that have images.

I was thinking about modeling the get_the_terms() function after the get_terms() function which has built in caching for the image files. But I’m a bit afraid of what would happen when this function was used in the loop in any archive template.

Basically the workflow would go something like this:

  1. Get all image/term associations (0 queries).
  2. Get terms associated with the global post: get_the_terms() (0 queries).
  3. Query for all images associated with the terms (3 queries). This is done by creating a list of all image ID’s and passing them to get_posts() via the include parameter.

IMHO this is fine to do in single views (single.php, page.php, etc.) but what would happen if a user was to use this function in the loop in an archive views? You will need to multiply 3 queries (produced by get_posts()) by the value of the posts-per-page setting (10 in a default WordPress installation). So now we’re at 30 total queries. It gets kinda ugly fast! Especially if users want images from more than one taxonomy displayed. 2 taxonomies per post would add 60 to what WordPress needs to do!

Basically there are a few paths to take moving forward and I would like to know what you would do in a situation like this.

  1. Do not build the functionality at all.

  2. Build the function and educate users that a caching plugin should be installed if they use get_the_terms() in archive views.

  3. Build the function + create another function that will cache all images before an archive template is loaded. This process would involve something like: After the $posts array has been generated (but before the template has been loaded), loop over all posts in the array calling get_the_terms() each iteration for every public taxonomy. The output of all term objects would be stored in a new array. Then, I could determine which images are associated with each term of every taxonomy and pass the image IDs to get_posts() in one shot. This would allow all image info to be cached for use in the template in 3 queries instead of 30, 60 or 90.

IMHO 3 seems like a logical way to go, but I’m not really sure if it would be an anti optimization or not.

Please let me know your thoughts about this!

1 Answer
1

I’d say your third option is the way to go.

Incidentally, why did you roll your own static cache in taxonomy_image_plugin_get_associations() instead of using the built-in WP_Cache API for that? Is there a reason wp_cache_get wouldn’t work here? Seems like using the WP object cache would optimize better when people do have caching plugins turned on.

Leave a Comment