1

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.

1 Answer 1

0

You 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.

4
  • Also see wordpress.stackexchange.com/a/39932/35003 about how metas are handled when executing a WP_Query Commented Jun 3, 2014 at 11:17
  • Use w3 total cache plugin.
    – ahmetlutfu
    Commented Jun 3, 2014 at 12:07
  • Thank you very much. I'll try to setup some kind of caching. I don't have server access (it's hosted) so I can't enable/install php extensions.
    – Heeties
    Commented Jun 5, 2014 at 15:15
  • APC is a very common PHP extension and should be available at most hosting providers, even on shared hosting.Plugins like WP super Cache or W3 Total Cache can also work on shared hosting. IF you have a FTP access you are good to go :) Commented Jun 5, 2014 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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