Query only Posts from Both of Two Category?

I have 3 categories,

1.OnGoing projects(cat id=’5′)

Read More

2.Completed Projects(cat id=’6′)

3.Upcoming Projects(cat id=’7′)

and also i have another 2 categories:(Not a Sub Category)

  1. chennai (cat id=’10’)

  2. Dubai (cat id=’11)

How to get the post from “OnGoing Projects” with “Chennai” category?(I like to Display -> OnGoing Projects on Chennai.)

I have tried the following code:

 <?php
 query_posts('posts_per_page=6&cat=5&cat=10');
while(have_posts()) : the_post();
?><li>
 <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(230,192)); ?></a>
</li>
<?php endwhile;
wp_reset_query();
?>

likely To show the “OnGoing Projects” with “Dubai” category? (Ongoing Projects on Dubai)
for this:

<?php
 query_posts('posts_per_page=6&cat=5&cat=11');
while(have_posts()) : the_post();
?><li>
 <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(230,192)); ?></a>
</li>
<?php endwhile;
wp_reset_query();
?>

This is Not Working Fully.

Please Help me to Figure Out.

Thanks i Advance.

Related posts

Leave a Reply

1 comment

  1. Just guessing here, but I suspect that query_posts() is not appropriate in this situation. query_posts() should only be used to modify the main query, and the emerging best practice is that query_posts() shouldn’t be used at all, but, instead, replaced by filtering pre_get_posts.

    Having said all that, I would look into WP_Query and particularly the tax_query argument.

    Your new snippet will look something like this:

    <?php
    $my_query_args = array(
        'posts_per_page' => 6,
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field' => 'id',
                'terms' => array( 5, 10 ),
                'operator' => 'AND'
            )
        )
    );
    
    $my_query = new WP_Query( $my_query_args );
    
    if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post(); ?>
    
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(230,192)); ?></a>
    </li>
    
    
    <?php endwhile; endif; wp_reset_postdata(); ?>