我正在尝试过滤循环以查找具有meta_key
具有特定的meta_value
. 我看了一下抄本,尝试了以下方法,但没有成功:
// No results
$args = array(
\'post_type\' => \'cqpp_interventions\',
\'posts_per_page\' => \'-1\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'meta_key\' => \'priority\',
/* Tried this too
\'meta_value\' => \'80\',
\'compare\' => \'=\'
*/
\'meta_value\' => array(\'80\'),
\'compare\' => \'IN\' )
)
);
// No results
$args = array(
\'post_type\' => \'cqpp_interventions\',
\'posts_per_page\' => \'-1\',
\'meta_key\' => \'priority\',
\'meta_value\' => 80
);
// This list me all cqpp_interventions and I can confirm that I have some with meta_value set to 80
$args = array(
\'post_type\' => \'cqpp_interventions\',
\'posts_per_page\' => \'-1\',
\'meta_key\' => \'priority\'
);
$cqpp_posts = get_posts( $args );
以下是我如何在循环内进行验证:
$priority = get_post_meta( get_the_ID(), \'priority\');
echo \'<pre>\';
var_dump($priority);
echo \'</pre>\';
其结果是:
search.php:16:
array (size=1)
0 =>
array (size=1)
0 => string \'80\' (length=2)
search.php:16:
array (size=1)
0 =>
array (size=2)
0 => string \'80\' (length=2)
1 => string \'91\' (length=2)
我能做些什么来解决这个问题?
SO网友:Aftab
从您的第一个$参数引用
$args = array(
\'post_type\' => \'cqpp_interventions\',
\'posts_per_page\' => \'-1\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'meta_key\' => \'priority\',
/* Tried this too
\'meta_value\' => \'80\',
\'compare\' => \'=\'
*/
\'meta_value\' => array(\'80\'),
\'compare\' => \'IN\' )
)
);
您已添加
\'relation\' => \'OR\'
, 这是不需要的。这个
relation
如果要查询多个元值,则需要添加。如果需要特定值,也不需要比较,因为默认情况下是使用
=
.
更新后的$参数将为
$args = array(
\'post_type\' => \'cqpp_interventions\',
\'posts_per_page\' => \'-1\',
\'meta_query\' => array(
array(
\'meta_key\' => \'priority\',
\'meta_value\' => \'80\', // since 80 is string
)
)
);
// try either of below.
$myPost = new WP_Query( $args ); // fetch post and managed in objects
$myPost = get_posts( $args ); // fetch post and store in array
希望这能帮到你。
谢谢