2

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; ?>
3
  • I worked it out: Place: query_posts(array('post__not_in' => $ids)); Above: while ( have_posts() ) : the_post();
    – Dan Lee
    Feb 10, 2012 at 12:40
  • @ChipBennett Ahem ;)
    – fuxia
    May 1, 2012 at 12:01
  • @toscho ...will move discussion to meta. May 1, 2012 at 13:42

1 Answer 1

3

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; ?>
1
  • 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(). May 1, 2012 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.