Forums

[resolved] Do not duplicate post covering 2 queries (35 posts)

  1. Alkorr
    Member
    Posted 1 year ago #

    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! :)

  2. Alkorr
    Member
    Posted 1 year ago #

    Any idea? :)

  3. Alkorr
    Member
    Posted 11 months ago #

    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! :)

  4. alchymyth
    The Sweeper
    Posted 11 months ago #

    http://codex.wordpress.org/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 http://pastebin.com/ and post the link to it here.

  5. Alkorr
    Member
    Posted 11 months ago #

    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!

  6. alchymyth
    The Sweeper
    Posted 11 months ago #

    if you need help with the details, please paste the full code of the template into a http://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)

  7. Alkorr
    Member
    Posted 11 months ago #

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

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

    Thanks ;)

  8. alchymyth
    The Sweeper
    Posted 11 months ago #

    http://pastebin.com/J1LTBd8H

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

    hopefully it will work with your posts ;-)

  9. Alkorr
    Member
    Posted 11 months ago #

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

  10. alchymyth
    The Sweeper
    Posted 11 months ago #

    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?

  11. Alkorr
    Member
    Posted 11 months ago #

    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! :)

  12. alchymyth
    The Sweeper
    Posted 11 months ago #

    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 http://pastebin.com/ and post the link to it here.

  13. Alkorr
    Member
    Posted 11 months ago #

    I pasted the full code of the third loop here: http://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! :)

  14. alchymyth
    The Sweeper
    Posted 11 months ago #

    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' )),
        );
  15. Alkorr
    Member
    Posted 11 months ago #

  16. alchymyth
    The Sweeper
    Posted 11 months ago #

    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)
        );
  17. Alkorr
    Member
    Posted 11 months ago #

    OMG!! You made my day alchymyth, I think it works perfectly now!!

    I'm so happy I can finally do what I REALLY want! Without your help I would still be stuck so thank you very much! This blog I'm working on is a personal project and I want it to be perfect and now it is! I'm so happy, thank you so much for your patience!! :)

    And I'm sure this code will help many other people like me using multiple loops. Thanks again!

  18. alchymyth
    The Sweeper
    Posted 11 months ago #

    you are welcome ;-)

    if everything is working fine, please mark this thread as 'resolved'

  19. Alkorr
    Member
    Posted 11 months ago #

    Maybe one last thing while I'm checking if everyting is working fine. If in the first loop, if I decide to show the last 3 posts (just like in the second loop) then these posts are the exact same ones as in the second loop.

    But with the no_duplicate check, they shouldn't right? If I call the 3 latest posts in both loop, once the 3 latests posts are called in the first loop, then the 3 from the second loop can't be duplicates. But apparently they are...

    I'm just asking because I just un-sticky the posts and by default the 3 latest posts are displayed in the first loop. If you don't see why then it's fine and I will close the topic ;)

    Thanks again alchymyth!

  20. alchymyth
    The Sweeper
    Posted 11 months ago #

    can you paste the full real template (nothing with HTML HERE or so) into a http://pastebin.com/ and post the link to it here?

  21. Alkorr
    Member
    Posted 11 months ago #

    Sure thing, here: http://pastebin.com/hUwFzPj9

    (love Pastebin BTW)

  22. alchymyth
    The Sweeper
    Posted 11 months ago #

    pastebin is great - and it takes really large files ;-)

    please paste everything (all three loops and all) from your template into the pastebin;

    i can't see anything wrong with the few lines from the last pastebin.

  23. Alkorr
    Member
    Posted 11 months ago #

    Full code here: http://pastebin.com/BGc1sGap :)

  24. alchymyth
    The Sweeper
    Posted 11 months ago #

    same mistake as before: this section in the last pastebin:

    SECOND LOOP
    
    <?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();

    -----
    a:
    delete this: $do_not_duplicate = array();

    (this is allowed only before the first loop)

    -----
    b:
    add the $do_not_duplicate back into 'post__not_in':

    $my_query = new WP_Query(array('posts_per_page' => 4, 'post__not_in' => array_merge($do_not_duplicate, get_option( 'sticky_posts' )) ) );

    -----
    pastebin corrected: http://pastebin.com/Mp9iHYiM

  25. Alkorr
    Member
    Posted 11 months ago #

    Hi Alchymyth, thanks, I modified my code but I still got the same problem: if I don't show any sticky, then the same posts are showing in my 1st and 2nd loop. Even with the code you provided.

    Plus now I get an error:

      Warning: array_merge() [function.array-merge]: Argument #1 is not an array in /alkorr/www/blog/wp-content/themes/v1/index.php on line 19

    The code is so intertwined... ;)

  26. alchymyth
    The Sweeper
    Posted 11 months ago #

    The code is so intertwined... ;)

    that is the reason i keep asking you to paste the full code of the template (the full index.php - all of it);
    so you don't have to fit the corrected pieces into the other parts of the template.

  27. Alkorr
    Member
    Posted 11 months ago #

    For the moment I focus on the loops so the HTML is quite simple.

    I pasted my index.php code here : http://pastebin.com/vpHLd1mW

  28. alchymyth
    The Sweeper
    Posted 11 months ago #

    this is again not the full code of index.php.

    i leave you with an outline of the basic structure:

    start:
    $do_not_duplicate = array(); //start with an empty array
    ---------------------
    query1 //don'u use anything with $do_not_duplicate in this first query
    loop1
    output of posts
    $do_not_duplicate[] = $post->ID; //collect the posts from the loop
    loop1-end
    ----------------------
    query2 // to exclude posts which have been shown in loop1;
           // use 'post__not_in' => $do_not_duplicate
           // or 'post__not_in' => array_merge($do_not_duplicate, get_option( 'sticky_posts' ))
    loop2
    output of posts
    $do_not_duplicate[] = $post->ID; //collect more posts from the loop
    loop2-end
    ----------------------
    
    query3 // to exclude posts which have been shown in loop1 and loop2;
           // use 'post__not_in' => $do_not_duplicate
           // or 'post__not_in' => array_merge($do_not_duplicate, get_option( 'sticky_posts' ))
    loop3
    output of posts
    $do_not_duplicate[] = $post->ID; //collect more posts from the loop
    loop3-end
    ----------------------

    my support ends here, if you don't paste the full code of your index.php into the next http://pastebin.com/ .

    i expect a full index.php to have a get_header() line and all three loops with all the html, exactly as you would use it in your theme.

    it is impossible to help, if you only paste fractions of your template, and if you don't follow the suggested changes to your code.

  29. Alkorr
    Member
    Posted 11 months ago #

    I pasted the full code of my index.php, the first loop was in the header, so now I moved it to my index.php so you can see my full template, all the three loops and the HTML code.

    Code here: http://pastebin.com/Fq8c4xE4

    If I forgot to modify something, sorry. I'm currently paying a lot of attention to the code you just posted in your previous message to better understand how multiple loops work together.

    I'm learning with great attention to what you say!

  30. alchymyth
    The Sweeper
    Posted 11 months ago #

    the first loop was in the header

    my bad - i was not aware of the way your theme is organized.

    if you look at the basic layout that i posted in my last reply, it might be easier to see how the multiple loops are working.

    it is too late for me now - i will check your code tomorrow.

Topic Closed

This topic has been closed to new replies.

About this Topic