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'm trying to work out how to exclude posts used in the first four custom queries in the last typical query.. haven't been able to find anything that will explain how to do this clearly.

My First four loops:

<?php 
// Loop 1
    $first_query = new WP_Query('category_name=funding&showposts=1'); // select category
    $ids = array(); 
    while($first_query->have_posts()) : $first_query->the_post(); ?>

        <?php the_title(); ?></br>

    <?php endwhile;


// Loop 2
    $second_query = new WP_Query(array('post__not_in' => $ids,'category_name' => 'industry','posts_per_page' => 1)); // select category
    while($second_query->have_posts()) : $second_query->the_post();
    $ids[] = get_the_ID(); ?>

        <?php the_title(); ?></br>

    <?php endwhile;


// Loop 3
    $third_query = new WP_Query(array('post__not_in' => $ids,'category_name' => 'open-consultations','posts_per_page' => 1)); // select category
    while($third_query->have_posts()) : $third_query->the_post();
    $ids[] = get_the_ID(); ?>

        <?php the_title(); ?></br>

    <?php endwhile;


// Loop 4
    $fourth_query = new WP_Query(array('post__not_in' => $ids,'category_name' => 'policy','posts_per_page' => 1)); // select category
    while($fourth_query->have_posts()) : $fourth_query->the_post();
    $ids[] = get_the_ID(); ?>

        <?php the_title(); ?>

    <?php endwhile; ?>

A typical WordPress query that I want to remove repeating posts from:

<?php if ( have_posts() ) : ?>

<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>

<a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'category-thumb', array('class' => 'alignleft')); } ?></a>              
    <strong style="float:right;"><?php the_time('l, F jS, Y') ?></strong>

    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

    <?php the_excerpt(); ?>

<hr>

<?php endwhile; ?>
<?php wp_pagenavi(); ?> <!-- Pagination -->

<?php endif; ?>
share|improve this question
    
I worked it out: Place: query_posts(array('post__not_in' => $ids)); Above: while ( have_posts() ) : the_post(); –  Dan Lee Feb 10 '12 at 12:40
5  
You should try to improve your 0 percent accept rate before asking more questions. –  Chip Bennett Feb 10 '12 at 13:29
    
@ChipBennett Ahem ;) –  toscho May 1 '12 at 12:01
    
@toscho ...will move discussion to meta. –  Chip Bennett May 1 '12 at 13:42
add comment

1 Answer

You have already figured it out but I will post an answer anyway for others.

To alter a Query, that has already been set, the easiest way is to use query_posts().

Note this will only work if run before the loop.

<?php
query_posts(array(
    'post__not_in' => array(1,2,3,4,'...')
));
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php // Loop content here ?>
<?php endwhile; else: ?>
    <?php // No posts message here ?>
<?php endif; ?>
share|improve this answer
    
The "missing link", then, is to save, in arrays, the $post->ID for each post returned by each secondary loop, then array_merge() those arrays, and pass that result to post__not_in in query_posts(). –  Chip Bennett May 1 '12 at 15:43
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.