Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

get_posts('numberposts=5&category=93+85')get_posts('numberposts=5&category=9385')

Is there a way to have get_posts grab only posts that are in BOTH categories, as above?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Use WP_Query's category__and:

$args = array(
    'posts_per_page' => 5,
    'category__and' => array( 93, 85 )
);

$my_query = new WP_Query( $args );
while( $my_query->have_posts() ):
    $my_query->the_post();
    the_title();
endwhile;

wp_reset_postdata();
share|improve this answer
    
Where would that go in a situation like this? <?php global $post; $myposts = get_posts('numberposts=5&category=1'); foreach($myposts as $post) : setup_postdata($post); ?> –  Sal Mar 6 '13 at 21:54
    
it would replace get_posts, see edit above. –  Milo Mar 6 '13 at 21:57
    
Hmm. Perhaps I mislead. I'm trying to only get posts that are in both categories at the same time. Category__and doesn't do that, no? –  Sal Mar 6 '13 at 22:09
    
it does, category__in fetches posts that are in one category OR another, category__and fetches posts that have both categories. –  Milo Mar 6 '13 at 22:32
    
Ah, alright. So now, and I apologize as I'm not too keen on PHP what would I replace this with: <?php global $post; $myposts = get_posts('numberposts=5&category=30'); foreach($myposts as $post) : setup_postdata($post); ?> <?php if ( in_category('1') ) { ?> <?php include('sidebar-reviews_review.php'); ?> <?php } ?> To make it pull posts from categories 30 and 1. –  Sal Mar 6 '13 at 22:35
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.