To avoid showing the same WordPress post in multiple loops, you can use a couple of methods. The most common approach is to keep track of the post IDs that have already been displayed and then exclude those posts from subsequent loops. Here’s a step-by-step guide on how to achieve this:

Method 1: Using Global Variable

  1. Initialize an Array to Store Displayed Post IDsBefore your first loop, initialize an empty array to keep track of the post IDs.

    php

    global $do_not_duplicate;
    $do_not_duplicate = array();
  2. First LoopIn your first loop, store the post IDs of the displayed posts in the global array.

    php

    <?php
    global $do_not_duplicate;
    $query1 = new WP_Query(array(
    'posts_per_page' => 5,
    ));
    if ($query1->have_posts()) :
    while ($query1->have_posts()) : $query1->the_post();
    $do_not_duplicate[] = get_the_ID();
    // Your loop code here
    endwhile;
    wp_reset_postdata();
    endif;
    ?>

  3. Second LoopIn your second loop, exclude the post IDs stored in the global array.

    php

    <?php
    global $do_not_duplicate;
    $query2 = new WP_Query(array(
    'posts_per_page' => 5,
    'post__not_in' => $do_not_duplicate,
    ));
    if ($query2->have_posts()) :
    while ($query2->have_posts()) : $query2->the_post();
    // Your loop code here
    endwhile;
    wp_reset_postdata();
    endif;
    ?>

Method 2: Using Transients for More Complex Scenarios

If you have more complex scenarios or need to persist the exclusion across different parts of your site, you might consider using WordPress transients.

  1. Set Up a TransientBefore your loops, check if a transient exists and use it. If not, initialize an empty array.

    php

    $do_not_duplicate = get_transient('excluded_posts') ? get_transient('excluded_posts') : array();
  2. First LoopStore the post IDs in the transient after your first loop.

    php

    <?php
    $query1 = new WP_Query(array(
    'posts_per_page' => 5,
    ));
    if ($query1->have_posts()) :
    while ($query1->have_posts()) : $query1->the_post();
    $do_not_duplicate[] = get_the_ID();
    // Your loop code here
    endwhile;
    wp_reset_postdata();
    set_transient(‘excluded_posts’, $do_not_duplicate, HOUR_IN_SECONDS);
    endif;
    ?>

  3. Second LoopExclude the post IDs stored in the transient in your second loop.

    php

    <?php
    $query2 = new WP_Query(array(
    'posts_per_page' => 5,
    'post__not_in' => $do_not_duplicate,
    ));
    if ($query2->have_posts()) :
    while ($query2->have_posts()) : $query2->the_post();
    // Your loop code here
    endwhile;
    wp_reset_postdata();
    endif;
    ?>

By following these methods, you can avoid displaying the same post in multiple loops on your WordPress site. The first method using a global variable is straightforward and works well for simple use cases. The second method using transients is more suitable for complex scenarios where exclusions need to persist across different parts of the site or for a longer duration.