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.