Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
Please rephrase the title of the question to reflect its nature rather than its context. –  Rarst May 21 '11 at 22:25
    
Would "How to best cache attachments while querying for terms?" work? Not sure how it should be titled. Open to suggestions. –  mfields May 21 '11 at 22:29
    
Hmmm... Maybe "How to organize and cache additional data, tied to terms?" –  Rarst May 22 '11 at 7:05

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
Hi Than! Thanks for your input here. I rarely access the WP_Cache API directly. only rarely when I have to do a custom post query with metadata - then I'll set the cache so that the template tags do not need to do extra work. Not sure about how static cache such as that in *_get_associations() would undermine caching plugins. Could you give an example? Personally, I've only used caching plugins that serve static html files to visitors. I've never used any that focus on server-level caching. –  mfields May 21 '11 at 17:06
    
I may not understand it properly, but I think that the static variable you're using will only be stored for the duration of the current page load. If you used the WP_Cache API instead and the site had a persistant object caching plugin enabled, then that variable could be stored in the cache. In this case it doesn't really matter since the results of get_option() are cached anyways, but I was just wondering. If you used the cache API on some of your other functions, like *_get_image_src(), then you could potentially let caching plugins handle a lot more of the persistant data. –  goldenapples May 21 '11 at 17:57
    
You're absolutely right (as far as I know). My intention here was to minimize the calls to the sanitize function during the time the script is executing. –  mfields May 21 '11 at 23:28
    
No one else seems to have an opinion on this, so going with #3 sounds like a good idea to me. Thanks for the input! –  mfields May 23 '11 at 12:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.