1

I am trying to make a filter with open and closed checkbox using Meta_Query. The value of metadatas is UNIX time, so the value of $opening_time and $closing_time is number like this - 234321543. It only works without $status_2, $opening_time_2, $closing_time_2. I think that something wrong with time logic.
How can i get it worked?

    $status =         'opening_hours_monday_day_status';
    $opening_time =   'opening_hours_monday_opening_time';
    $closing_time =   'opening_hours_monday_closing_time';
    $status_2 =       'opening_hours_monday2_day_status';
    $opening_time_2 = 'opening_hours_monday2_opening_time';
    $closing_time_2 = 'opening_hours_monday2_closing_time';
    $current_time =   strtotime('2016-01-01 ' . current_time('h:i a'));
    $element_filter_arr = array();
             if ($timings == 'open') {
                    $element_filter_arr[] = array(
                        'key' => $status,
                        'value' => 'on',
                        'compare' => '=',
                    );
                    $element_filter_arr[] = array(
                        'key' => $opening_time,
                        'value' => $current_time,
                        'compare' => '<=',
                    );
                    $element_filter_arr[] = array(
                        'key' => $closing_time,
                        'value' => $current_time,
                        'compare' => '>=',
                    );
                    $element_filter_arr[] = array(
                        'key' => $status_2,
                        'value' => 'on',
                        'compare' => '=',
                    );
                    $element_filter_arr[] = array(
                        'key' => $opening_time_2,
                        'value' => $current_time,
                        'compare' => '<=',
                    );
                    $element_filter_arr[] = array(
                        'key' => $closing_time_2,
                        'value' => $current_time,
                        'compare' => '>=',
                    );
                } else if ($timings == 'close') {
                    $element_filter_arr[] = array(
                        'relation' => 'OR',
                        array(
                            'key' => $status,
                            'value' => 'off',
                            'compare' => '=',
                        ),
                        array(
                            'key' => $opening_time,
                            'value' => $current_time,
                            'compare' => '>',
                        ),
                        array(
                            'key' => $closing_time,
                            'value' => $current_time,
                            'compare' => '<',
                        ),
                    );
                }

$args = array(
                    'posts_per_page' => "-1",
                    'post_type' => 'shop',
                    'post_status' => 'publish',
                    'fields' => 'ids',
                    'meta_query' => array(
                        $element_filter_arr,
                    ),
Tod
  • 187
  • 3
  • 13

1 Answers1

0

Can you try this for open condition

if ($timings == 'open') {       
   $meta_query_args = array(
     'relation' => 'OR',
    array(
    'relation' => 'AND',
        array(
            'key' => $status,
            'value' => 'on',
            'compare' => '=',
        ),
        array(
            'key' => $opening_time,
            'value' => $current_time,
            'compare' => '<=',
        ),
        array(
            'key' => $closing_time,
            'value' => $current_time,
            'compare' => '>=',
        ),
    ),
    array(
    'relation' => 'AND',
        array(
            'key' => $status2,
            'value' => 'on',
            'compare' => '=',
            ),
        array(
            'key' => $opening_time2,
            'value' => $current_time,
            'compare' => '<=',
        ),
    array(
        'key' => $closing_time2,
        'value' => $current_time,
        'compare' => '>=',
    ),
    ),
    );
   }
$meta_query = new WP_Meta_Query( $meta_query_args );
Sudharshan Nair
  • 1,152
  • 1
  • 10
  • 24