WordPress.org

Ready to get started?Download WordPress

Forums

Get next 5 posts after Post ID? (5 posts)

  1. matthewordie
    Member
    Posted 2 years ago #

    When I'm on my single post page, I'd like to have a related posts area and show the next 5 posts that follow the current one viewed. I've been reading through the Codex and haven't found a good way to accomplish this.

    Any ideas?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    The next 5 by? date? Category? Tag?

  3. matthewordie
    Member
    Posted 2 years ago #

    I think it'd be by tag. I'm using a custom taxonomy to tag my posts.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    Purely theoretical but what about:

    $args = array( 'taxonomy_name_here' => 'taxonomy_value_here', 'orderby' => 'date');
    $custom_query = new WP_Query($args);
    $c = 0;
    foreach ( $custom_query as $custom_post ):
    $c++;
    if( $custom_post->ID = $post->ID ) break; // we've found our post
    $args = array( 'taxonomy_name_here' => 'taxonomy_value_here', 'orderby' => 'date', offset' => $c
    );
    $recent_query = new WP_Query($args);
    [ do usual loop stuff ]
  5. matthewordie
    Member
    Posted 2 years ago #

    Thanks, that got me started. I added some other PHP magic and got it sorted. I was unfamiliar with the WP_Query function good to know!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.