admin管理员组文章数量:1122832
I've been working on this project where I need to load events (custom post type) and a whole bunch of meta data for every event. The problem is that page loading times are getting longer and longer when more and more events get added.
At the start with only a few events this wasn't a problem at all, but now when there are 100+ events in the system page loads take up to 6 seconds which is too long.
Example of what I'm doing: Show all the events in the system
(All below is done inside a plugin)
I load all events with get_posts and specify the custom post type.
I loop through all the posts and get all the meta data with get_post_meta
in a single call.
When testing where the bottleneck was, I found out that when I remove get_post_meta
execution time went down from 4.5 seconds to 14ms.
I also tested what would happen if I load every meta data separately like so: get_post_meta($post->ID, "meta_value", true);
If I have a view where I only need a few meta values this speeds up the process to where I could cut 20% of the loading time, but when I need almost all, there was no change in loading times.
I also noticed that using get_post_custom
doesn't really help in this case.
After looking around online (here, blogs, etc..), people pointed out that when you do a get_posts
, all meta data is automatically retrieved and cached. But it seems this isn't true when I look at the execution time and amount of queries that are processed.
Is there any way of speeding this up further via php/mysql?
One thing I was thinking of was: is there a way of getting all posts and meta data for each post in a single call. But I didn't have any luck of finding something online that would help me achieving this.
I've been working on this project where I need to load events (custom post type) and a whole bunch of meta data for every event. The problem is that page loading times are getting longer and longer when more and more events get added.
At the start with only a few events this wasn't a problem at all, but now when there are 100+ events in the system page loads take up to 6 seconds which is too long.
Example of what I'm doing: Show all the events in the system
(All below is done inside a plugin)
I load all events with get_posts and specify the custom post type.
I loop through all the posts and get all the meta data with get_post_meta
in a single call.
When testing where the bottleneck was, I found out that when I remove get_post_meta
execution time went down from 4.5 seconds to 14ms.
I also tested what would happen if I load every meta data separately like so: get_post_meta($post->ID, "meta_value", true);
If I have a view where I only need a few meta values this speeds up the process to where I could cut 20% of the loading time, but when I need almost all, there was no change in loading times.
I also noticed that using get_post_custom
doesn't really help in this case.
After looking around online (here, blogs, etc..), people pointed out that when you do a get_posts
, all meta data is automatically retrieved and cached. But it seems this isn't true when I look at the execution time and amount of queries that are processed.
Is there any way of speeding this up further via php/mysql?
One thing I was thinking of was: is there a way of getting all posts and meta data for each post in a single call. But I didn't have any luck of finding something online that would help me achieving this.
Share Improve this question edited Jun 3, 2014 at 11:43 Pieter Goosen 55.4k23 gold badges115 silver badges209 bronze badges asked Jun 3, 2014 at 11:05 HeetiesHeeties 113 bronze badges1 Answer
Reset to default 0You should use an object cache, it will definitely speed up the get_post_meta queries as they will be cached when doing the initial WP_Query (get_posts).
I recommend testing with APC cache, using the APC Object Cache Backend : https://wordpress.org/plugins/apc/
Just drop the file in your wp-content folder and make sure you have APC extension available.
本文标签: wp querygetpostmeta slowing down my page load (in a plugin)
版权声明:本文标题:wp query - get_post_meta slowing down my page load (in a plugin) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283027a1926831.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论