WordPress.org

Forums

[resolved] How to add four WP Queries without duplicate posts (7 posts)

  1. Pritam
    Member
    Posted 4 years ago #

    Hi,

    I want to have four separate WP queries on index.php without any duplicate posts. How can we do that?

    Tks

  2. Pritam
    Member
    Posted 4 years ago #

    So far I have come across tuts that tell you how to have two queries without duplicate posts. I want four.

  3. alchymyth
    Forum Moderator
    Posted 4 years ago #

    just keep repeating the 'do_not_duplicate' array method in each loop:

    http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

    mainly after this line: 'Note for Multiple Posts in the First Category'

    rough layout:

    query for loop1
    loop1:
    $do_not_duplicate[] = $post->ID;
    end loop1;
    
    query for loop2 including 'post__not_in => $do_not_duplicate'
    loop2:
    $do_not_duplicate[] = $post->ID;
    end loop2;
    
    query for loop3 including 'post__not_in => $do_not_duplicate'
    loop3:
    $do_not_duplicate[] = $post->ID;
    end loop3;
    
    query for loop4 including 'post__not_in => $do_not_duplicate'
    loop4:
    end loop4;
  4. Pritam
    Member
    Posted 4 years ago #

    Thanks for pointing me in the right direction.
    I was using 'post__not_in' => array($post->ID,$exclude_ids),
    argument instead of 'post__not_in' => $exclude_ids,
    Now it's working fine.

  5. Alkorr
    Member
    Posted 4 years ago #

    Hi there, alchymyth! I tried to use this function but it doesn't work for me. I'm using the code provided by MichaelH on this thread (displaying 5 latest post title on the main content) and my goal is that if an article is posted in two categories (Books + Movies), then once it is displayed on the first one (Books), then on the second one (Movies), it won't show; the next article from that category will be displayed instead.

    I'm not good at PHP, nothing new here but I always search and try everything I can. Now I'm stuck...

    Thanks for you help!

  6. alchymyth
    Forum Moderator
    Posted 4 years ago #

    code by MichaelH extended with 'do_not_duplicate'
    http://pastebin.com/bC7MQwNa

    (no guarantee, untested)

  7. Alkorr
    Member
    Posted 4 years ago #

    OMG it works!! Thank you so much alchymyth. You made my day! :)

Topic Closed

This topic has been closed to new replies.

About this Topic