logo

$5
Query posts only if in BOTH categories

Normally, I use this to query posts in specific categories:

<?php $recent = new WP_Query("cat=6,38&showposts=0"); while($recent->have_posts()) : $recent->the_post();?>

<h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>

<?php endwhile; ?>


but I need to query posts only if they are in BOTH of these categories categories.

Meredith Marsh | 01/07/10 at 7:53pm | Edit


(2) Possible Answers Submitted...

  • avatar
    Last edited:
    01/07/10
    8:07pm
    Ron Rennick says:

    Add a category__and=2 to your query:

    new WP_Query("cat=6,38&category__and=2");

  • avatar
    Last edited:
    01/07/10
    8:31pm
    Utkarsh Kukreti says:

    Use this

    <?php $recent = new WP_Query(array('category__and' => array(6, 38))); while($recent->have_posts()) : $recent->the_post();?>
    <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
    <?php endwhile; ?>


This question has expired.





Current status of this question: Completed