Woocommerce:仅显示开始日期和结束日期之间的产品

时间:2018-01-17 13:21:36

标签: wordpress woocommerce

我正在尝试仅显示两个日期之间的产品。当我只放置一个数组和一个日期时它可以工作,但是当我放入其他数组并且我的第二个日期它不再起作用时,它会显示所有产品。有什么想法吗?

function custom_meta_query( $meta_query ){
$today = current_time('Ymd');
 $args = array (
    'meta_query' => array(
'relation' => 'AND',
    array(
        'key'=>'flash_sale_start',
        'value' => $today,
        'compare'=>'<=',
    'type' => 'DATE'
    ),
       array(
    'key'=>'flash_sale_end',
        'value' => $today,
        'compare'=>'>=',
    'type' => 'DATE'
    )),);
$date_query = new WP_Query( $args );
    //return $meta_query;
}

// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
    if( ! is_admin() )
        return custom_meta_query( $meta_query );
}

// The shortcode products query
add_filter( 'woocommerce_shortcode_products_query', 'custom__shortcode_products_query', 10, 3 );
function custom__shortcode_products_query( $query_args, $atts, $loop_name ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}

// The widget products query
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}

谢谢

1 个答案:

答案 0 :(得分:1)

我找到了解决方案,这是一个array()问题

function custom_meta_query( $meta_query ){
$today = current_time('Ymd');
 $args = array (
    'numberposts' => -1,
    'meta_query' => array(
    'relation' => 'AND',
    'start_clause' => array(
        'key'=>'flash_sale_start',
        'value' => $today,
        'compare'=> '<=',
     'type' => 'DATE'
    ),
         'end_clause' => array(
         'key' => 'flash_sale_end',
     'value' => $today,
         'compare' => '>=',
     'type' => 'DATE'
     ),
       ));

return $args;
}

// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
    if( ! is_admin() )
        return custom_meta_query( $meta_query );
}

// The shortcode products query
add_filter( 'woocommerce_shortcode_products_query', 'custom__shortcode_products_query', 10, 3 );
function custom__shortcode_products_query( $query_args, $atts, $loop_name ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}

// The widget products query
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}