• Resolved Alkorr

    (@alkorr)


    Hi! I wonder if it is possible to not duplicate a post using 2 different queries. I explain.

    I first want to show the 4 latest posts. Then I want to show the latest posts from each category. I’ve found a way (thanks to alchymyth) not to have duplicated post within the query showing the latest posts from each category. But the posts listed in my 4 latest posts query are duplicated in the latest posts list from each category. Logical.

    Now I try to find a way to avoid that. I tried to put this code before the first query (showing the 4 latest posts):

    <?php $my_query = new WP_Query('posts_per_page=4');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>

    And close it after the query for the latest posts from each category:

    <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    
      <?php endwhile; endif; ?>

    But it doesn’t work.

    It may sound complicated but I’m sure I’m not the only one looking for a solution about these duplicated posts on the home page.

    Thanks for your help! ??

Viewing 15 replies - 1 through 15 (of 34 total)
  • Thread Starter Alkorr

    (@alkorr)

    Any idea? ??

    Thread Starter Alkorr

    (@alkorr)

    Hi! It’s been 2 months now I asked for help in this forum and since then I worked on a solution but I couldn’t make it work neither…..

    I changed the query and added:

    $args=array(
          'posts_per_page' => 1,
          'post__not_in' => $do_not_duplicate,
          'post__not_in' => get_option( 'sticky_posts' ),
          'category__in' => array($category->term_id),

    But I still get duplicates because I have another query on my page, a different one, and the same post is showing twice…

    Any help would be so great, thanks a lot! ??

    https://codex.www.ads-software.com/The_Loop#Multiple_Loops_in_Action

    for ‘do-not-duplicate’ with several loops and posts, try the ‘array method’ described in the above linked docu described after ‘Note for Multiple Posts in the First Category’;

    (the first loop – example):

    <?php $do_not_duplicate = array();
      $my_query = new WP_Query('posts_per_page=4');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate[] = $post->ID; ?>

    (the second loop – rough example, make sure to use the right query args):

    <?php $args=array(
          'posts_per_page' => 1,
          'post__not_in' => array_merge($do_not_duplicate, get_option( 'sticky_posts' )),
          'category__in' => array($category->term_id)
    );
     $my_query = new WP_Query( $args );
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate[] = $post->ID; ?>

    (any further loop – rough example, the query args need adjusting):

    <?php $args=array(
          'posts_per_page' => 1,
          'post__not_in' => array_merge($do_not_duplicate, get_option( 'sticky_posts' )),
          'category__in' => array($category->term_id)
    );
     $my_query = new WP_Query( $args );
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate[] = $post->ID; ?>

    i hope this gives you the idea how to use the method.

    if you need help with the details, please paste the full code of the template into a https://pastebin.com/ and post the link to it here.

    Thread Starter Alkorr

    (@alkorr)

    Hi alchymyth! Thanks, this code looks very helpful and I’m trying to replace my actual code with it. But I’m already stuck: in the first loop, how do I do to exclude all sticky posts?

    My query uses:

    ‘post__not_in’ => get_option( ‘sticky_posts’ )

    I don’t know how to make it work within your loop. I tried some code from the Codex but it shows sticky posts and doesn’t exclude them from the loop… I feel dumb for asking, sorry, but I’m learning ??

    Thanks!

    if you need help with the details, please paste the full code of the template into a https://pastebin.com/ and post the link to it here.

    please do the above, so i can see the full code.

    in the first loop, try:

    <?php $do_not_duplicate = array();
      $my_query = new WP_Query(array('posts_per_page' => 4, 'post__not_in' => get_option( 'sticky_posts' ) ) );
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate[] = $post->ID; ?>

    (untested)

    Thread Starter Alkorr

    (@alkorr)

    Sure, here is the link: https://pastebin.com/diYiFYMD

    I hope it will help you. I don’t see what I am doing wrong…

    Thanks ??

    https://pastebin.com/J1LTBd8H

    hope you can check what i’ve changed in the code;

    hopefully it will work with your posts ??

    Thread Starter Alkorr

    (@alkorr)

    Great, it seems to work! But there is a little problem: on the third loop, the latest post from each category doesn’t come from the good category. For example: the review of game appears in the category Movies, and so on. It worked fine with the old code so maybe what you changed had a side effect?

    Apparently it’s working fine so far, now I only have to see the third loop working to tell you if it’s perfect ??

    Again, thanks a lot…

    on the third loop, the latest post from each category doesn’t come from the good category.

    are the posts in more than one category?

    Thread Starter Alkorr

    (@alkorr)

    Hi alchymyth! Nope, they are only in one category, no duplicates, no sticky so far so it works fine. But now, instead of showing:

      Post title: Review of Halo
      Category: Games

      Post title: Review of RED
      Category: Movies

    It shows:

      Post title: Review of RED
      Category: Games

      Post title: Review of Halo
      Category: Movies

    I looked at the code and I don’t understand why the post is not from the right category. Do you have an idea of what the problem might be?

    Thanks! ??

    in the third loop, try to change:

    foreach($categories as $category) {
        $args=array(
          'posts_per_page' => 1,

    to:

    foreach($categories as $category) {
        $args=array(
          'numberposts' => 1,

    and please paste the full code of the third loop into a https://pastebin.com/ and post the link to it here.

    Thread Starter Alkorr

    (@alkorr)

    I pasted the full code of the third loop here: https://pastebin.com/7Jrssy5X

    I made the change you suggested but it didn’t change anything.

    Also the posts are not anymore classified by Category (Alphabetically) but by date. Maybe this is the problem here and it explains why the post about a movies is showing instead of a post about games, and so on…

    Hope it helps! ??

    please paste the real section that you are actually using in your template.
    not the one with HTML CODE HERE

    in you pasted version, you are missing the ‘category__in’ parameter for the ‘get_posts()’ loops.

    $args=array(
         'numberposts' => 1,
            'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
        );
    Thread Starter Alkorr

    (@alkorr)

    the category paramter in the $args is missing;

    try:

    $args=array(
         'numberposts' => 1,
            'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
    'cetagory__in' => array($category->term_id)
        );
Viewing 15 replies - 1 through 15 (of 34 total)
  • The topic ‘Do not duplicate post covering 2 queries’ is closed to new replies.