0

I have a simple meta_query to get events out that have a meta field start_date and end_date that fits within the a filtered start and end date.

This is the meta_query:

    $get_array['meta_query'] = array(

        'relation' => 'OR',

        array(
            'relation' => 'AND',
            array(
                'key' => 'end_date',
                'compare' => '>=',
                'value' => convert_date($_GET['start_date'], 'd-m-Y', 'Ymd')
            ),
            array(
                'key' => 'start_date',
                'compare' => '<=',
                'value' => convert_date($_GET['start_date'], 'd-m-Y', 'Ymd')
            )
        ),

        array(
            'relation' => 'AND',
            array(
                'key' => 'end_date',
                'compare' => '>=',
                'value' => convert_date($_GET['end_date'], 'd-m-Y', 'Ymd')
            ),
            array(
                'key' => 'start_date',
                'compare' => '<=',
                'value' => convert_date($_GET['end_date'], 'd-m-Y', 'Ymd')
            )
        ),

        array(
            'relation' => 'AND',
            array(
                'key' => 'start_date',
                'compare' => '>=',
                'value' => convert_date($_GET['start_date'], 'd-m-Y', 'Ymd')
            ),
            array(
                'key' => 'end_date',
                'compare' => '<=',
                'value' => convert_date($_GET['end_date'], 'd-m-Y', 'Ymd')
            )
        )

    );

It is very slow and takes 21 seconds to run!

The rest of the get_posts array uses pagination, order by meta_value start_date and categories... so there's a lot it's trying to do. However it wasn't slow until I added the meta_query.

What can I do to speed this up? I've google'd it but there are none that reference dates and some have multiple answers that aren't too helpful.

Will I have to resort to running my own custom SQL? Thanks

  • It's because you made the relation complex try remapping the the query – Jaed Mosharraf Oct 12 '17 at 9:06
  • Thanks for replying, what do you mean by try remapping the the query ? – Jack Nicholson Oct 12 '17 at 14:38
  • Try reducing the use of relation – Jaed Mosharraf Oct 12 '17 at 14:53
  • I cannot reduce the amount of ORs otherwise the logic won't be correct and will return the incorrect data :/ – Jack Nicholson Oct 12 '17 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.