How to exclude posts that already appear in the main loop, from the category loop

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"
    h2Latests 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 --
            h3a 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"
    h2Popular 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 --
            h3a 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(); ?

Topic exclude posts Wordpress

Category Web


<?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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.