The Problem
WordPress Meta Query isn't as flexible as the regular query as far as picking dates. The WP Query can pick dates by the month number, the week day name, and many other options, which would be suited well for what I need.
ACF Fields
Event Date, End Date, Death Date (Certain Taxonomies), "Repeat (Forever, Until)"
Basically I constructed a meta query that will get the dates that land on today, or that may be recurring (Hence Daily, Monthly and Weekly) and then I filter everything via PHP by doing compares to the weekday name for weekly, and the day number for monthly. I also check to make sure that any recurring posts are not showing up when they are not supposed to be.
Here is the meta query array I've been using the date is stored as Ymd
:
'relation' => 'OR', [ 'key' => 'event_date', 'value' => $date->format('md'), 'compare' => 'LIKE', 'type' => 'numeric' ], [ 'key' => 'death_date', 'value' => $date->format('md'), 'compare' => 'LIKE', 'type' => 'numeric' ], [ 'key' => 'how_often', 'value' => array('Daily', 'Monthly', 'Weekly'), 'compare' => 'IN' ]
To put it into perspective, this query will retrieve ~130 posts, including meta and other related queries, and after the PHP has ran to filter the posts (which takes 2.5 seconds!) I am left with 78 posts.
What I've Tried
I've tried limiting the query more by being very specific for the recurring posts, like specifying that the event_date
must be <=
the queried date, and either the end_date
is >=
the queried date or the meta forever
is set to forever
. However this won't help when it comes to the monthly or weekly recurring as I have no way to feasibly calculate those dates. I already have over 10k posts loaded and using this custom meta structure. It ran fine with up to about 1200 posts then after that the site became very sluggish. the page with 78 posts currently takes around 8-12 seconds for the first byte to load.
I've tried caching this data, but someone could hit any day in the calendar and it might not be cached, which would cause the user to be held up for 8-12 seconds before they see anything from the site.
Questions
- What is the best way to go about retrieving the monthly and weekly recurring posts via SQL?
- Will that actually speed up my page load or will it slow it down? Obviously with more posts comes more queries for meta, but pagination isn't really an option for this application.