On a site I built, I opted to use custom fields instead of a calendar plugin to organize my posts in a "calendar". In the beginning this seemed like the most logical way to accomplish what I needed. However since importing all the content, things didn't turn out like I expected. My "Day" view takes 2-4 seconds to load ~60-100 items, and my month view takes 15-22 seconds to load ~900 items.
Every post has a
event_date field that is stored as
YYYYmmdd. Some posts have the option to repeat on a fixed or infinite basis like normal calendar events would. I set up two fields for this, one called
repeat with options
until. Along with this is a field for on what interval it repeats
how_often. The values for that are
Yearly. And last but not least some posts have an
end_date to specify the end of the event.
Some posts based on another taxonomy are "auto recurring" so to speak and recur on a yearly basis.
The easiest way to get this data and filter it down was to use this meta_query:
<?php $mq = ['meta_query' => [ 'relation' => 'OR', [ 'key' => 'event_date', 'value' => $date->format('md'), 'compare' => 'LIKE', 'type' => 'numeric' ], [ 'key' => 'how_often', 'value' => array('Daily', 'Monthly', 'Weekly'), 'compare' => 'IN' ] ] ]
It does a LIKE for the currently queried date's month and day and another OR for all recurring posts.
Then use a function (omitted) to test each post to see if it was valid for the current day. Then the data is then passed to the view to be shown to the user.
I'm not used to dealing with EAV tables or WordPress in general when it comes to building something custom like this. I'd prefer to have built everything using custom tables but I feared that was going to be harder to implement with WordPress and would limit expansion in the future.
- What else can I do to the query to do as little work as possible to eliminate "extra" posts in PHP that get queried because my query is too broad.
- For the month view, I'm only displaying the post categories, the event_date, the title, and the total amount of posts per day. Is it possible to only query for this data to speed up the query?
- If all else fails, what's a better way to handle this type of data in WordPress? I'd prefer to stay away from calendar plugins and use "Posts" in some way.
Thanks for reading