使用元查询的WP用户查询

时间:2016-10-10 作者:Nas Atchia

比较整数值“>=”或“<;=”时遇到一些问题使用meta\\u查询和WP\\u USER\\u查询。

假设我已使用以下元值存储了以下元键:

meta_key            meta_value

type                type1     
status              active
location            London
min_price           10000
max_price           20000
对于元键“type”、“status”和“location”,查询工作正常。但我对“最低价格”和“最高价格”有问题。

下面是我用来检索具有上述元键的用户列表的代码:

// Values
$type = \'type1\';
$status = \'active\';
$location = \'London\';
$price = 15000;

// Query args
$args = array(
   \'role\' => \'end-user\',
   \'orderby\' => \'ID\',
   \'order\' => \'ASC\',
   \'meta_query\' => array(
       \'relation\' => \'AND\',
        array(
            \'key\' => \'type\',
            \'value\' => $type,
            \'compare\' => \'=\'
        ),
        array(
            \'key\' => \'status\',
            \'value\' => $status,
            \'compare\' => \'=\'
        ),
        array(
            \'key\' => \'location\',
            \'value\' => $location,
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\' => \'min_price\',
            \'value\' => $price,
            \'compare\' => \'>=\',
            \'type\' => \'NUMERIC\'
        ),
        array(
            \'key\' => \'max_price\',
            \'value\' => $price,
            \'compare\' => \'<=\',
            \'type\' => \'NUMERIC\'
        ),
   )
);

// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($args);

// Get the results
$users = $wp_user_query->get_results();

// Check if we have users
if (!empty($users)) {
    // Loop through results
    foreach ($users as $user) {
        echo $user->ID;
    }
} else {
    echo \'No Users Found!\'; 
}    
此查询返回“找不到用户!”。但是,如果我使用为“min\\U price”和“max\\U price”元查询存储的相同值,它就可以工作。E、 g.:

array(
    \'key\' => \'min_price\',
    \'value\' => 10000,
    \'compare\' => \'>=\',
    \'type\' => \'NUMERIC\'
),
array(
    \'key\' => \'max_price\',
    \'value\' => 20000,
    \'compare\' => \'<=\',
    \'type\' => \'NUMERIC\'
),
10000和20000以外的其他值不起作用。请帮忙。

1 个回复
SO网友:websupporter

事实上,我想你的密码是

哪里min_price >= $价格和max_price <;=$价格

尝试切换操作数,如下所示:

    array(
        \'key\' => \'min_price\',
        \'value\' => $price,
        \'compare\' => \'<=\',
        \'type\' => \'NUMERIC\'
    ),
    array(
        \'key\' => \'max_price\',
        \'value\' => $price,
        \'compare\' => \'>=\',
        \'type\' => \'NUMERIC\'
    ),
它适用于10.000和20.000,因为您使用了较大的/相等运算符和较低的/相等运算符。

相关推荐