I’ve a meta question in get_posts that is taking a ridiculously very long time to finish. It really works advantageous, however it simply takes approach too lengthy.
I’ve a customized put up sort named occasion
. On each occasion
put up, there may be customized meta knowledge:
- _post_sort_date (occasion date in
YmdHis
format, used for sorting) - _post_date_year (occasion 12 months)
- _post_date_month (occasion month)
- _post_date_day (occasion day)
What I must do is get the subsequent occasion that is the subsequent month or larger, relative to the $12 months
and $month
variables. So if $12 months = 2021
and $month = 10
(October 2021) then it ought to discover the primary occasion that is in November 2021 or later.
My question beneath works advantageous, however it’s slooooooow. It actually takes about 40 seconds to execute and I don’t know why.
$next_event = get_posts( array(
'post_type' => 'occasion',
'post_status' => 'publish',
'numberposts' => 1,
'fields' => 'ids',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => '_post_date_year',
'worth' => $12 months,
'examine' => '=',
'sort' => 'numeric',
),
array(
'key' => '_post_date_month',
'worth' => $month,
'examine' => '>',
'sort' => 'numeric',
),
),
array(
'key' => '_post_date_year',
'worth' => $12 months,
'examine' => '>',
'sort' => 'numeric',
),
),
'meta_key' => '_post_sort_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
) );
I am guessing I’ve structured the question in a foul approach and that is why it is so gradual, however I am unable to work out the best way to velocity it up.
UPDATE: Sorry, I forgot so as to add that there are solely about 10 occasion posts. Additionally, I feel I really simply mounted it by reversing the order of the OR situations. After doing that it runs at a wonderfully regular (principally immediate) velocity. If anybody know why that mounted it, I’d like to know.