我已经找了一段时间,但还没有找到适用于此的解决方案。
我希望能够使用Wordpress函数“get_posts()”来检索属于两个不同类别的任何帖子(我的意思是AND而不是OR)。如果我用这样的逗号分隔:
'category'=>'1,2';
它似乎可以检索属于CAT 1或CAT 2的帖子,而不是CAT 1和CAT 2.如何使用get_posts执行此操作?
我已经将此视为一种解决方案,但它似乎并不适用于我:
'category__and' => array(1,2)
感谢。
答案 0 :(得分:0)
我认为你不能用get_posts()
执行此操作,因为get_posts()
只会调用1个查询(没有子查询),并且您需要一个子查询来确定要跳过哪些行,具体取决于所需的条件。
您必须遵循以下代码片段:
$desiredCategory = array( 2, 6 );
$finalPostID = array();
$allPosts = get_posts( array('category__and' => $desiredCategory) );
foreach ($allPosts as $postVariable)
{
$categories = get_the_category($postVariable->ID);
if( in_array($desiredCategory, $categories) )
$finalPostID [] = $postVariable;
}
// Now you can use $finalPostID as your collection of posts.