Get Next / Prev 3 Posts in Relation to Current Post of WordPress

I have 7 prescription eyeglasses posts, like this:

1 – How to buy eyeglasses online?
2 – Why so many people buy prescription glasses online?
3 – How come it saves so much shopping glasses online?
4 – this is the current post
5 – Do you know the importance of prescription sunglasses?
6 – It is very important to wear sports eyeglasses to protect your eyes!
7 – How to choose a suitable eyeglass frame online?

As noted, number four is the current post being displayed. I need to create a query that will allow me to display the previous 3 posts (by publication date) and also the three posts after. This can be done with two separate queries.

Here is the method, two steps, first add the following code to functions.php:

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;
}

Second, add the following code at suitable place of single.php:

<?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 />’;
echo “<a href=”.get_permalink( $p->ID ).”>”.get_the_title( $p->ID ).”</a><br />”;

$next = $siblings[‘next’];

foreach( $next as $p )
// echo get_the_time( ‘d m Y’, $p ) . ‘: ‘ . apply_filters( ‘the_title’, $p->post_title ) . ‘<br />’;
echo “<a href=”.get_permalink( $p->ID ).”>”.get_the_title( $p->ID ).”</a><br />”;
?>