我试图将一个值数组传递到meta\\u查询值中。
// Works as expected to return posts with 1474 in featured
\'meta_query\' => array(
array(
\'key\' => \'featured\',
\'value\' => 1474,
\'compare\' => \'LIKE\'
)
)
// Works as expected to return posts with 2213 in featured
\'meta_query\' => array(
array(
\'key\' => \'featured\',
\'value\' => 2213,
\'compare\' => \'LIKE\'
)
)
但是,当我设置为数组时,它不能正常工作。我试图返回“特色”设置为1474或2213的帖子。
// Does not work, wp_query returns all posts
\'meta_query\' => array(
array(
\'key\' => \'featured\',
\'value\' => array(1474, 2213),
\'compare\' => \'LIKE\'
)
)
我也变了,喜欢进去,但没有成功。我希望不要使用多个meta\\u查询语句,因为数组的长度可能会改变。
SO网友:darrinb
我手动向两个不同的帖子添加了一个“特色”元键,值分别为1474和2213,然后运行了一个基本的WP\\u Query(),如下所示:
$qv = [
\'meta_query\' =>[
[
\'key\' => \'featured\',
\'value\' => [1474, 2213],
\'compare\' => \'IN\',
]
]
];
$r = new WP_Query($qv);
它拉起了两根柱子。这是它生成的查询:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1
AND (
( wp_postmeta.meta_key = \'featured\' AND wp_postmeta.meta_value IN (\'1474\',\'2213\') )
)
AND wp_posts.post_type = \'post\'
AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\')
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 12
var_dump()
拿出你的
WP_Query()
并查看正在运行的sql查询WP。