开发者

WordPress 3.1: Using query_posts to do an AND search of categories

I have posts that are categorised and can currently use the query_posts() function to do an开发者_开发知识库 OR search of posts based on their category, for example I can get all posts that are in category 1 OR 2 by calling query_posts('cat=1,2&orderby=date&showposts=5');

However I would like to do an AND search, so just getting posts that are in category 1 AND 2 instead.

Could anyone tell me how to do it please? WordPress documenentation mentions passing a category__and array of category ids, however this doesn't seem to work in 3.1. Do I need to use a custom SELECT query maybe or does WordPress have a neat way to do this?

Thank you.


See the documentation query_posts

query_posts( array( 'category__and' => array(1,3), 'posts_per_page' => 2, 'orderby' => 'title', 'order' => 'DESC' ) );

(this combination will return posts belong to both Category 1 AND 3, showing just two (2) posts, in descending order by the title)


query_posts( array( 'category__and' => array(1,3), 'posts_per_page' => 2, 'orderby' => 'title', 'order' => 'DESC' ) );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜