WordPress.org

Support

Support » How-To and Troubleshooting » Get next 5 posts after Post ID?

Get next 5 posts after Post ID?

  • 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?

Viewing 4 replies - 1 through 4 (of 4 total)
  • esmi

    @esmi

    Forum Moderator

    The next 5 by? date? Category? Tag?

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

    esmi

    @esmi

    Forum Moderator

    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 ]

    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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get next 5 posts after Post ID?’ is closed to new replies.
Skip to toolbar