meta_query,为什么它不起作用=或?

时间:2018-02-20 00:18:16

标签: arrays wordpress relation

<?php
$param_1 = array( 'key' => 'width_picture', 'value' => array( $_GET['width_min'], $_GET['width_max'] ), 'compare' => 'BETWEEN');
$param_2 = array( 'key' => 'height_picture', 'value' => array( $_GET['height_min'], $_GET['height_max'] ), 'compare' => 'BETWEEN');
$param_3 = array( 'key' => '_price', 'value' => array( $_GET['price_min'], $_GET['price_max'] ), 'compare' => 'BETWEEN');

$massSort['relation']= 'OR';
$massSort[] = array($param_1, $param_2, $param_3);
var_dump($massSort);

$args = array( 
    'post_type' => 'product', 
    'posts_per_page' => -1,
    'orderby' =>'date',
    'order' => 'DESC',
    'meta_query' => array($massSort)
);  

1 个答案:

答案 0 :(得分:0)

More info

$meta_query = array('relation' => 'OR');

if (!empty($_GET['price_min']) && !empty($_GET['price_max'])):
    $meta_query[] = array(
        array(
            'key'       => '_price',
            'value'   => array( $_GET['price_min'], $_GET['price_max']  ),
            'type'    => 'numeric',
            'compare' => 'BETWEEN',
        ),
    );
endif;

if (!empty($_GET['height_min']) && !empty($_GET['height_max'])):
$meta_query[] = array(
    array(
        'key'       => 'height_picture',
        'value'   => array( $_GET['height_min'], $_GET['height_max']),
        'type'    => 'numeric',
        'compare' => 'BETWEEN',
    ),
);
endif;

if (!empty($_GET['width_min']) && !empty($_GET['width_max'])):
$meta_query[] = array(
    array(
        'key'       => 'width_picture',
        'value'   => array(  $_GET['width_min'], $_GET['width_max'] ),
        'type'    => 'numeric',
        'compare' => 'BETWEEN',
    ),
);
endif;

$args = array( 
    'post_type' => 'product', 
    'posts_per_page' => -1,
    'orderby' =>'date',
    'order' => 'DESC',
    'meta_query' => meta_query
);