admin管理员组文章数量:1287829
My theme calls the update_meta_cache() function 58 times per page! This function appears to run the following query based on the post in question:
SELECT post_id, meta_key, meta_value
-> FROM wp_postmeta
-> WHERE post_id IN (81649)
-> ORDER BY meta_id ASC
-> ;
The vast majority of the meta_keys (and corresponding values) returned are not necessary for the page in question (e.g. yoast_wpseo_title, _edit_last, _edit_lock are not needed for the homepage loop).
Is there a way to reduce or prevent calling of the update_meta_cache? Perhaps a way to include in a function on the pre_get_posts hook?
My theme calls the update_meta_cache() function 58 times per page! This function appears to run the following query based on the post in question:
SELECT post_id, meta_key, meta_value
-> FROM wp_postmeta
-> WHERE post_id IN (81649)
-> ORDER BY meta_id ASC
-> ;
The vast majority of the meta_keys (and corresponding values) returned are not necessary for the page in question (e.g. yoast_wpseo_title, _edit_last, _edit_lock are not needed for the homepage loop).
Is there a way to reduce or prevent calling of the update_meta_cache? Perhaps a way to include in a function on the pre_get_posts hook?
Share Improve this question asked May 6, 2015 at 9:50 JoshJosh 1271 gold badge3 silver badges11 bronze badges 1- Just a sidenote: Explanation of update_post_(meta/term)_cache. – Howdy_McGee ♦ Commented Feb 1, 2016 at 16:32
2 Answers
Reset to default 2Whenever you use the function get_post_meta()
, the above query will be performed to get all post meta and store in the cache. The more posts you query, the more queries are made.
To reduce the number of queries, we should pre-cache all meta values of all posts in the query before calling get_post_meta
.
This is the sample code taken from a tutorial:
add_filter( 'posts_results', 'cache_meta_data', 9999, 2 );
function cache_meta_data( $posts, $object ) {
$posts_to_cache = array();
// this usually makes only sense when we have a bunch of posts
if ( empty( $posts ) || is_wp_error( $posts ) || is_single() || is_page() || count( $posts ) < 3 )
return $posts;
foreach( $posts as $post ) {
if ( isset( $post->ID ) && isset( $post->post_type ) ) {
$posts_to_cache[$post->ID] = 1;
}
}
if ( empty( $posts_to_cache ) )
return $posts;
update_meta_cache( 'post', array_keys( $posts_to_cache ) );
unset( $posts_to_cache );
return $posts;
}
For a WP_Query
, caching can be disabled by setting certain caching parameters to false
.
Similarly for WP_Term_Query
, it can be disabled by including update_term_meta_cache => false
in the query args.
Additionally, the filter update_{$meta_type}_metadata_cache can be used to disable metadata caching for all queries on the object type. (ex. update_post_metadata_cache
, update_term_metadata_cache
, etc)
本文标签: wp queryReduce or prevent calling of updatemetacache
版权声明:本文标题:wp query - Reduce or prevent calling of update_meta_cache 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741327115a2372551.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论