开发者

Wordpress: Find posts in category 1 and not in category 2

I have two categories, visible and hideMe...

I want to list all posts in the visible category, which is easy enough. However, due to user error (or oversight) there may be some posts which are in both categories.

How can I pull the posts that are in the visible category but not also in the hideMe category?

Here's my current solution. I'm just not certain that the get_pos开发者_运维问答ts method is the least expensive option I could use...

$cat=get_cat_ID('visible');
$cat2=get_cat_ID('hideMe');
$myposts = get_posts(array('cat' => "$cat,-$cat2",'showposts' => 5));
if($myposts )
{//do something}


I'm not sure about get_posts, but have you checked out query_posts?

See Template Tags/query posts

It works the same way as get_posts, not sure if it's less "expensive" but give it a try

$catnow = 13; //replace with category id you want to pull 
$exception = ',-19,-18'; //replace with category ids you want to exclude
$post_args = array('cat'=>$catnow.$exception);
query_posts($post_args); 
if (have_posts()): while (have_posts()): the_post();
//do something to the posts
endwhile; 
else: 
//no posts found
endif;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜