Problems with using multiple conditional meta_query

Question

I’m trying to use multiple meta_query for a custom search but one condition cause infinite loading. The results of the query will never display from the moment I add it.

I do know that the conflict comes from the other date meta query. I tried to remove point by point each condition and it is the date conditions “start_date” and “end_date” which causes the conflict but I can’t figure out why. The section who cause the problem is commented at the end juste before the $args array déclaration.

I know that the code could seem absurd to you but for the good of all, the query exceeding the 500 lines of coding and of conditions I post here only the part which generates a conflict

(PS. I’ve already check all the meta_key and everything is OK)

I’m pretty sure that i’m doing something wrong.

Thanks !

function search_custom() {

$tax_query_array = array('relation' => 'AND');
$meta_query_array = array('relation' => 'AND');

    switch($_GET['select-date']) {
        case '1';
            $current_date = date('Ymd');

            array_push($meta_query_array, array(
                'relation' => 'OR', 
                array(
                'relation' => 'AND', 
                    array(
                        'key' => 'start_date',
                        'compare' => 'NOT EXISTS',
                        'value' => ''
                    ), 
                    array(
                        'key' => 'end_date', 
                        'compare' => 'NOT EXISTS',
                        'value' => ''
                    )
                ), 
                array(
                    'key' => 'end_date',
                    'value' => $current_date,
                    'type' => 'DATE',
                    'compare' => '>='
                )
            ));
        break;
    }

/* THIS SECTION CAUSING INFINITE LOADING WHEN PASSSING ON ARGS[] */

        $current_date = date('Ymd');
        array_push($meta_query_array, 
            array(
                'relation' => 'OR', 
                array(
                    'relation' => 'AND', 
                    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_publish',
                        'value' => $current_date,
                        'type' => 'DATE',
                        'compare' => '<='
                    ),
                    array(
                        'key' => 'end_date_publish',
                        'value' => $current_date,
                        'type' => 'DATE',
                        'compare' => '>='
                    )
                )
            )
        );

    $args = array(
        'post_type' => array('lpc_event','lpc_place'),
        'posts_per_page' => -1,
        'tax_query' => $tax_query_array,
        'meta_query' => $meta_query_array,
        'geo_query' => $geo_query_array,
        'orderby' => 'distance',
        'order'   => 'DESC'
    );

    $custom_query = new WP_Query( $args ); 

EDIT : Problem solved !

I check the condition which does not pass and causes a heavy load outside the query.

Searching on multiple metas inside a query is not a good “architecture”. Best way is to create own table, avoid superfluous or use $wpdb.

0
, , FloJDM 4 years 2020-03-09T23:50:47-05:00 0 Answers 111 views 0

Leave an answer

Browse
Browse