• Short question:

    How do you query_posts in cat 1 AND not in cat 2?

    The detailed version:

    I have the following post categories:

    College (cat 1)
    Corporate (cat 10)
    Idea of the Week (cat 13)

    I have a College home page and a Corporate home page.

    On the College home page, I want to display the most recent College blog post and the most recent Idea of the Week. They are visually separated.

    So for Recent College, this should pull only the most recent post in the category College, but not in the category Idea of the Week.

    <?php query_posts( posts_per_page=1&orderby=date&order=DESC&cat=1&cat=-13' ); the_post(); ?> 
    
    <a href="<?php the_permalink(); ?>"><h4><?php the_title(  ); ?></h4></a>
    <?php the_post_thumbnail( 'medium' ); ?>
    <p><?php the_excerpt(); ?></p>
    <a href="<?php the_permalink(); ?>">Read more</a>
    
    <?php wp_reset_query(); ?>

    I thought this might work, but this appears to pull for either in cat 1 OR not in cat 13. But what I need is in cat 1 AND not in cat 13.

    For Recent Idea of the Week, this should display the most recent post that is in category College AND category Idea of the week.

    I’m using:

    <?php query_posts( array( 'category__and' => array(1,13), 'posts_per_page=1&orderby=date&order=DESC' )); the_post(); ?>

    This seems to work as expected.

  • The topic ‘query_post in category 1 AND not in category 2’ is closed to new replies.