Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query posts only if in BOTH categories WordPress

  • SOLVED

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.

Answers (2)

2010-01-08

Utkarsh Kukreti answers:

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; ?>


2010-01-08

Ron Rennick answers:

Add a category__and=2 to your query:

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