1

我正在尝试使用 Meta_Query 制作带有打开和关闭复选框的过滤器。metadatas 的值是 UNIX 时间,所以 $opening_time 和 $closing_time 的值是这样的数字 - 234321543。它只在没有 $status_2、$opening_time_2、$closing_time_2 的情况下有效。我认为时间逻辑有问题。
我怎样才能让它工作?

    $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,
                    ),
4

1 回答 1

0

你能在开放条件下试试这个吗

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 );
于 2018-07-13T11:44:28.283 回答