0

I'm trying to search in two different post_type "places" and "events" but one parameter is about dates and places don't have this meta.

Also, event got 4 different dates : a start_date and end_date AND start_date_publish and end_date_publish who's here for determinate the moment when the event can be display on the site or on the search form results.

How can i make this query more light for searching on this two post type in one time ?

Here is the code that crashed the query because it's taking too much time to search :

(PS. Sry for this bad english)

array_push($meta_query_array, array(
            'relation' => 'OR', 
            array(
                array(
                    'key' => 'start_date', 
                    'compare' => 'NOT EXISTS',
                    'value' => ''
                ),
                array(
                    'key' => 'end_date', 
                    'compare' => 'NOT EXISTS',
                    'value' => ''
                ),
                array(
                    'key' => 'start_date_publish',
                    'compare' => 'NOT EXISTS',
                    'value' => ''
                ), 
                array(
                    'key' => 'end_date_publish', 
                    'compare' => 'NOT EXISTS',
                    'value' => ''
                )

            ), 
            array(
            'relation' => 'AND', 
                array(
                    'key' => 'start_date',
                    'value' => $current_date,
                    'type' => 'DATE',
                    'compare' => '<='
                ),
                array(
                    'key' => 'end_date',
                    'value' => $current_date,
                    'type' => 'DATE',
                    'compare' => '>='
                ),
                array(
                    'key' => 'start_date_publish',
                    'value' => $current_date,
                    'type' => 'DATE',
                    'compare' => '<='
                ),
                array(
                    'key' => 'end_date_publish',
                    'value' => $current_date,
                    'type' => 'DATE',
                    'compare' => '>='
                ),
            )
        ));
3
  • 1
    How about running two separate queries, then merging the arrays in PHP afterward?
    – WebElaine
    Commented Apr 8, 2020 at 14:01
  • This is an extremely expensive query, I'd switch as much of those as possible to using taxonomies. A hidden taxonomy with a has_date term would allow you to replace the super super heavy/expensive NOT EXISTS clauses with a super fast taxonomy term check. You should also look into eliminating either start_date, or start_date_publish, the same with end_date
    – Tom J Nowell
    Commented Apr 8, 2020 at 14:07
  • @WebElaine I've take this solution, and it's work. Thx
    – FloJDM
    Commented Apr 15, 2020 at 7:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.