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.

I have 3 categories,

1.OnGoing projects(cat id='5')

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.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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(); ?>
share|improve this answer
    
Thank you Very Much @:mrwweb. wp_query is most powerful tool in wordpress. :) must appreciate. –  Ramkumar May 11 '12 at 9:29
add comment

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.