[Plugin: Taxonomy Metadata] Caching meta data
-
I’ve found this plug-in very useful and will be using it as the basis of a custom metadata table that I need for a plug-in I’m working on.
I just wanted to suggest maybe caching the term meta?
Of course the metadata functions you use, already cache the term’s metadata – but they need to first need to generate the cache. As it stands each ‘new’ call to
get_metadata
will require a query of the database to generate the cache of that term.I wanted to suggest hooking into the hooks
get_terms
(for when taxonomy terms are queried) andwp_get_object_term
(for when terms associated with some posts are queried). These both pass an array of terms – allowing you to callupdate_meta_cache
with an array of term IDs to generate the cache for all those terms at once.So, when posts are queried – the metadata of all the associated terms will generated to cache in one query – and then inside the loop all calls to retrieve the metdata will be obtained from cache.
Pseudo code:
add_filter('get_terms','update_term_cache'); add_filter('wp_get_object_terms','update_term_cache'); function update_term_cache($terms){ $taxonomy ='mytaxonomy'; //Set taxonomy $term_ids = wp_list_pluck($terms,'term_id'); update_meta_cache($taxonomy,$update_cache); return terms; }
If the term’s metadata is already in cache, then
update_meta_cache
doesn’t do anything – so it’s not wasteful.https://www.ads-software.com/extend/plugins/taxonomy-metadata/
- The topic ‘[Plugin: Taxonomy Metadata] Caching meta data’ is closed to new replies.