Wordpress - Get Next / Prev 3 Posts in Relation to Current Post

This can be done in a single query, though i can't speak specifically about how well this query will perform(i've not spent a great deal with Union queries - never had a need, till now)..

First, a function to select two sets of results, but using union to return them as a single result set.

function get_post_siblings( $limit = 3, $date = '' ) {
    global $wpdb, $post;

    if( empty( $date ) )
        $date = $post->post_date;

    //$date = '2009-06-20 12:00:00'; // test data

    $limit = absint( $limit );
    if( !$limit )
        return;

    $p = $wpdb->get_results( "
    (
        SELECT 
            p1.post_title, 
            p1.post_date,
            p1.ID
        FROM 
            $wpdb->posts p1 
        WHERE 
            p1.post_date < '$date' AND 
            p1.post_type = 'post' AND 
            p1.post_status = 'publish' 
        ORDER by 
            p1.post_date DESC
        LIMIT 
            $limit
    )
    UNION 
    (
        SELECT 
            p2.post_title, 
            p2.post_date,
            p2.ID 
        FROM 
            $wpdb->posts p2 
        WHERE 
            p2.post_date > '$date' AND 
            p2.post_type = 'post' AND 
            p2.post_status = 'publish' 
        ORDER by
            p2.post_date ASC
        LIMIT 
            $limit
    ) 
    ORDER by post_date ASC
    " );
    $i = 0;
    $adjacents = array();
    for( $c = count($p); $i < $c; $i++ )
        if( $i < $limit )
            $adjacents['prev'][] = $p[$i];
        else
            $adjacents['next'][] = $p[$i];

    return $adjacents;
}

There's a test date in there, you can safely ignore that or add in your own value for testing.

Here's some sample code you can use in your single.php loop to list out the results, though note this is just a generic example, and the function might need to select more/different data, but based on the info you've provided i wasn't sure exactly what you wanted, so the following is for illustration and to give a sample you can use to test the results..

<?php 
$siblings = get_post_siblings( 3 ); // This is the same as doing the call below(which is just for illustration)
//$siblings = get_post_siblings( 3, $post->post_date );

$prev = $siblings['prev'];

foreach( $prev as $p )
    echo get_the_time( 'd m Y', $p ) . ': ' . apply_filters( 'the_title', $p->post_title ) . '<br />';

$next = $siblings['next'];

foreach( $next as $p )
    echo get_the_time( 'd m Y', $p ) . ': ' . apply_filters( 'the_title', $p->post_title ) . '<br />';
?>

Awaiting feedback... :)


This can be done much better with the date_query property of WP_Query class. This will get the posts before the publish date of the current post

// WP_Query arguments
$args = array (
    'post_type'              => 'post',
    'post_status'            => 'publish',
     'date_query'    => array(
        'column'  => 'post_date',
        'before'   => get_the_date()
    ),
);

// The Query
$the_query = new WP_Query( $args );

.........


3 x get_adjacent_post():

global $post;
$current_post = $post; // remember the current post

for($i = 1; $i <= 3; $i++){
  $post = get_previous_post(); // this uses $post->ID
  setup_postdata($post);

  // do your stuff here       
  the_title();

}

$post = $current_post; // restore

same for next 3 posts, just change the function to get_next_post()...


To do this with a single query, while still using the WP API, try changing the LIMIT value to 3 within the get_previous_post_sort and get_next_post_sort filters.