wp-query loads forever when meta-key is missing

Im struggling with an query that should show posts in a date range given by advanced custom fields repeater date field. So my repeater fields have a start and end date:

24.05.2018 – 28.05.2018

01.06.2018 – 05.06.2018

The query works fine as long the keys exists (even if its empty)

$query_args = array(
‘post_type’ => ‘custompost’,
‘post_status’ => ‘publish’,
‘meta_query’ => array(
‘relation’ => ‘OR’,
array(
‘relation’ => ‘AND’,
array(
‘key’ => ‘date_0_start’,
‘compare’ => ‘>=’,
‘value’ => ‘20180520’
),
array(
‘key’ => ‘date_0_end’,
‘compare’ => ‘<=’,
‘value’ => ‘20180701’
)
),
array(
‘relation’ => ‘AND’,
array(
‘key’ => ‘date_1_start’,
‘compare’ => ‘>=’,
‘value’ => ‘20180520’
),
array(
‘key’ => ‘date_1_end’,
‘compare’ => ‘<=’,
‘value’ => ‘20180701’
)
)
)
);

Now I have post with more or less repeating dates. When a meta-key is missing (e.g. date_2_start and date_2_end) the page loads forever and ends in a error 500.

How can I accomplish a “flexible” query that works even when a key is missing? I need this flexibility to make sure that posts could have up to 10 repeating dates even if the max of existing posts would only have 8 repeating dates.

Help would be highly appreciated.

Read more here:: wp-query loads forever when meta-key is missing

Leave a Reply

Your email address will not be published. Required fields are marked *