WordPress.org

Ready to get started?Download WordPress

Forums

[How To] Exclude Current Posts from 2nd loop (3 posts)

  1. westondeboer
    Member
    Posted 5 years ago #

    I am pulling 5 random posts and i didn't want it to show the current post in this loop, so here is how to do it:

    <?php
    global $wp_query;
    $thePostID = $wp_query->post->ID;
    $rand_posts = get_posts('numberposts=5&exclude='.$thePostID.'&orderby=rand');
     foreach( $rand_posts as $post ) :?>
    
    <h3><?php the_title(); ?></h3>
     <?php endforeach; ?>
  2. enailor
    Member
    Posted 5 years ago #

    Let's say I want to have 2 loops... the first one displays the most recent 5 posts, the second a random selection of posts; however I want the most recent 5 to be excluded from the second loop. How would you go about doing that?

  3. Dunkkan
    Member
    Posted 4 years ago #

    Hey, excellent ! Thanks very much for this.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.