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… 🙂