Wordpress Meta_Query打开关闭时间

时间:2018-07-11 22:50:37

标签: php wordpress metadata post-meta

我正在尝试使用Meta_Query创建一个带有打开和关闭复选框的过滤器。 元数据的值是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,
                    ),

1 个答案:

答案 0 :(得分: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 );