0

In my front-page I have 2 loops:

  1. the first one is for latest 20 posts in all categories
  2. the second one is for latest 6 posts in one specific category

How can I remove from the second loop (specific category loop) those that already appear in the first loop (general loop)?

I know that I have to use post__not_in => array($excludeID) in my second loop, but I can't catch all those id's in the first loop.

Here is my code so far:

    <?php get_header(); ?>

<section class="content latest">
    <h2>Latests 20 posts</h2>
    <ul>
    <?php $the_query = new WP_Query( 'posts_per_page=20' ); ?>
    <?php $excludeID = array(); ?>

    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    <?php $excludeID = $post->ID; ?>

        <li>
            <!-- post thumbnail linking to the single post page -->
            <?php if ( has_post_thumbnail() ) : ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_post_thumbnail(); ?>
            </a>
            <?php endif; ?>

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

            <!-- date -->
            <p class="date">Posted on: <?php the_time( 'j F Y' ); ?></p>

            <!-- display the post excerpt -->
            <p><?php the_excerpt(__('(more…)')); ?></p>
        </li>

        <?php endwhile;
            wp_reset_postdata();
        ?>
    </ul>
</section>

<h1><?php echo $excludeID ?></h1>

<section class="content category__popular">
    <h2>Popular posts</h2>
    <ul>
    <?php
        $args = array(
            'post_type' => 'post',
            'orderby' => 'date',
            'order' => 'DESC',
            'posts_per_page' => 6,
            'category_name' => 'popular',
            'paged' => get_query_var('paged'),
            'post_parent' => $parent,
            'post__not_in' => array($excludeID)
        );

        $popularquery = new WP_Query($args); ?>

    <?php while($popularquery->have_posts()) : $popularquery->the_post(); ?>

        <li>
            <!-- post thumbnail linking to the single post page -->
            <?php if ( has_post_thumbnail() ) : ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_post_thumbnail(); ?>
            </a>
            <?php endif; ?>

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

            <!-- date -->
            <p class="date">Posted on: <?php the_time( 'j F Y' ); ?></p>

            <!-- display the post excerpt -->
            <p><?php the_excerpt(__('(more…)')); ?></p>
        </li>

        <?php endwhile;
            wp_reset_postdata();
        ?>
    </ul>
</section>


<?php get_footer(); ?>
7
  • 1
    Catch post id inside an array. $excludeID[] = get_the_ID(). And on the 2nd query, pass that array of ids. 'post__not_in' => $excludeID
    – Shazzad
    May 11 '20 at 22:29
  • I'm already doing it, you can see it in my code. Then if I print the $excludeID there's just the last post id. May 11 '20 at 22:32
  • Please take a closer look at the code of my comments.
    – Shazzad
    May 11 '20 at 22:34
  • $excludeID[] is an array and you have declared it as a variable which is why you are getting only the ID of the last post. May 11 '20 at 22:36
  • I declared it as an array before the first while loop, and after it i tried to catch them.. By the way i replaced <?php $excludeID = $post->ID; ?> with your $excludeID[] = get_the_ID() , but now the array is empety. May 11 '20 at 22:41
1
<?php get_header(); ?>

<section class="content latest">
    <h2>Latests 20 posts</h2>
    <ul>
    <?php $the_query = new WP_Query( 'posts_per_page=20' ); ?>

    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    <?php $excludeID[] = $post->ID; ?>

        <li>
            <!-- post thumbnail linking to the single post page -->
            <?php if ( has_post_thumbnail() ) : ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_post_thumbnail(); ?>
            </a>
            <?php endif; ?>

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

            <!-- date -->
            <p class="date">Posted on: <?php the_time( 'j F Y' ); ?></p>

            <!-- display the post excerpt -->
            <p><?php the_excerpt(__('(more…)')); ?></p>
        </li>

        <?php endwhile;
            wp_reset_postdata();
        ?>
    </ul>
</section>

<h1><?php print_r( $excludeID); ?></h1>

<section class="content category__popular">
    <h2>Popular posts</h2>
    <ul>
    <?php
        $args = array(
            'post_type' => 'post',
            'orderby' => 'date',
            'order' => 'DESC',
            'posts_per_page' => 6,
            'category_name' => 'popular',
            'paged' => get_query_var('paged'),
            'post_parent' => $parent,
            'post__not_in' => $excludeID
        );

        $popularquery = new WP_Query($args); ?>

    <?php while($popularquery->have_posts()) : $popularquery->the_post(); ?>

        <li>
            <!-- post thumbnail linking to the single post page -->
            <?php if ( has_post_thumbnail() ) : ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_post_thumbnail(); ?>
            </a>
            <?php endif; ?>

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

            <!-- date -->
            <p class="date">Posted on: <?php the_time( 'j F Y' ); ?></p>

            <!-- display the post excerpt -->
            <p><?php the_excerpt(__('(more…)')); ?></p>
        </li>

        <?php endwhile;
            wp_reset_postdata();
        ?>
    </ul>
</section>


<?php get_footer(); ?>

Since it's an array I am using print_r() function to displaying all the IDs. I haven't tested the second loop. Please check if it's working for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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