在wordpress帖子中,我有自定义的元字段(发货地点、保证金价格)。我想通过这些元字段过滤帖子。Ship from字段包含多个值(例如:产品可以从中国和美国发货)。当我选择中国时,查询工作正常,但当我选择美国时,查询工作不正常。
我正在试图找出query的问题所在。
查询如下:
$args = array(
\'post_type\' => \'post\',
\'meta_query\' => array(
array(
\'key\' => \'ship-from\',
\'value\' => \'usa\',
\'compare\'=> \'in\'
),
array(
\'key\' => \'profit-margin\',
\'value\' => array(0,100),
\'meta_type\' => \'numeric\'
\'compare\' => \'BETWEEN\'
)
)
);
$result = new WP_Query($args);
以下是WP\\U query执行的最后一个查询:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN
wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN
wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1 AND (
( wp_postmeta.meta_key = \'ship-from\' AND wp_postmeta.meta_value IN
(\'usa\') ) AND ( mt1.meta_key = \'profit-margin\' AND mt1.meta_value
BETWEEN \'0\' AND \'50\' ) ) AND wp_posts.post_type = \'post\' AND
((wp_posts.post_status = \'publish\')) GROUP BY wp_posts.ID ORDER BY
wp_posts.post_date DESC LIMIT 0, 12
如有任何意见,我们将不胜感激。
SO网友:Max Yudin
你失踪了relation
内部参数meta_query
阵列as described 在代码参考中。
此外,当您使用\'compare\'=> \'IN\'
, 这个\'ship-from\'
值应为数组。
$args = array(
\'post_type\' => \'post\',
\'meta_query\' => array(
\'relation\' => \'AND\', // note this!
array(
\'key\' => \'ship-from\',
\'value\' => \'usa\',
\'compare\'=> \'IN\'
),
array(
\'key\' => \'profit-margin\',
\'value\' => array(0,100),
\'meta_type\' => \'numeric\'
\'compare\' => \'BETWEEN\'
)
)
);