Query only Posts from Both of Two Category?

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="https://wordpress.stackexchange.com/questions/51850/<?php the_permalink(); ?>"><?php the_post_thumbnail(array(230,192)); ?></a>
</li>


<?php endwhile; endif; wp_reset_postdata(); ?>

Leave a Comment