所以,我有一个自定义循环,它似乎在任何地方都能工作,只有一个地方。
$args = array(
\'post_type\' => \'post\',
\'meta_key\' => \'wpcf-post-weight\',
\'orderby\' => \'meta_value_num title\',
\'meta_query\' =>
array(
array(
\'key\' => \'wpcf-featured\',
\'value\' => -1,
),
array(
\'key\' => \'wpcf-news-expire-date\',
\'value\' => $today,
\'compare\' => \'>=\',
),
),
/*\'cat\'=> \'8\',*/
\'posts_per_page\' => 1
);
$alertloop = new WP_Query( $args );
注释掉cat=>8后,查询变量如下所示
Array
(
[post_type] => post
[meta_key] => wpcf-post-weight
[orderby] => meta_value_num title
[meta_query] => Array
(
[0] => Array
(
[key] => wpcf-featured
[value] => -1
)
[1] => Array
(
[key] => wpcf-news-expire-date
[value] => 1383047207
[compare] => >=
)
)
[posts_per_page] => 1
[error] =>
[m] =>
[p] => 0
[post_parent] =>
然而,只要我取消对cat=>8的注释,它就会删除第一个meta\\u查询。
Array
(
[post_type] => post
[meta_key] => wpcf-post-weight
[orderby] => meta_value_num title
[meta_query] => Array
(
[0] => Array
(
[key] => wpcf-news-expire-date
[value] => 1383046582
[compare] => >=
)
)
[cat] => 8
[posts_per_page] => 1
[error] =>
[m] =>
这在Wordpress 3.7中。我不认为这有什么区别,但我尝试重新排列$args订单,但都没有用。任何见解都将不胜感激。非常感谢。
SO网友:sosukeinu
不知道为什么会这样,因为另一个查询失败了。我不确定这是否是其他人正在经历的错误。每当我包含任何类别的“inclusive”参数时,它都会破坏我的meta\\u查询,但如果我是“排除”类别,它似乎不会这样做。所以生成所有类别ID的数组,然后取消设置我想在查询中“包括”的类别。然后将数组设置为“category\\u not\\u in”参数似乎正在工作。
我仍然想知道为什么会发生这种情况。非常感谢。
$cats = get_all_category_ids();
$removeKeys = array(8,10,11,12);
foreach($removeKeys as $key) {
if (in_array($key, $cats))
{
unset($cats[array_search($key, $cats)]);
}
}
$args = array(
\'post_type\' => \'post\',
\'meta_query\' =>
array(
array(
\'key\' => \'wpcf-featured\',
\'value\' => -1,
),
array(
\'key\' => \'wpcf-news-expire-date\',
\'value\' => $today,\'compare\' => \'>=\',
),
),
\'category__not_in\'=> $cats,
\'posts_per_page\' => 1
);
$alertloop = new WP_Query( $args );