最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览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(); ?>

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(); ?>
Share Improve this question asked May 11, 2020 at 22:25 Antonino LatteneAntonino Lattene 1431 gold badge2 silver badges12 bronze badges 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 Commented May 11, 2020 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. – Antonino Lattene Commented May 11, 2020 at 22:32
  • Please take a closer look at the code of my comments. – Shazzad Commented May 11, 2020 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. – made2popular Commented May 11, 2020 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. – Antonino Lattene Commented May 11, 2020 at 22:41
 |  Show 2 more comments

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论