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 />”;
?>

 

 

The above method may not work for some WordPress versions.

 

Here is another method to meet our demands. Add the following code to single.php:

 

<!– ————get 5 previous and next posts begin—— –>
<div class=”navigation post-navigation”>

<?php
global $post;
$current_post = $post; // remember the current post

for($i = 1; $i <= 5; $i++):
$post = get_previous_post(); // this uses $post->ID
setup_postdata($post);
?>
<a href=”<?php echo get_permalink( $post->ID ); ?>”>
<?php echo apply_filters( ‘the_title’, $post->post_title ); ?>
</a><br>

<?php
// the_title();
// the_content();
endfor;
$post = $current_post;
?>

<hr>
<?php
global $post;
$current_post = $post; // remember the current post

for($i = 1; $i <= 5; $i++):
$post = get_next_post(); // this uses $post->ID
setup_postdata($post);
?>
<a href=”<?php echo get_permalink( $post->ID ); ?>”>
<?php echo apply_filters( ‘the_title’, $post->post_title ); ?>
</a><br>

<?php
// the_title();
// the_content();
endfor;
$post = $current_post;
?>
</div>
<!– ————get 5 previous and next posts end—— –>

 

I have tested the code under https://www.optical4u.com

 

The second method is still having an issue, if there is no next, or previous post, errors will come!

 

Now, let me share the best method:

 

<!– ————get 5 previous and next posts begin—— –>
<div class=”navigation post-navigation”>

<?php
global $post;
$current_post = $post; // remember the current post

for($i = 1; $i <= 5; $i++):
$post = get_previous_post(); // this uses $post->ID
if ( is_a( $post , ‘WP_Post’ ) ) : ?>
<a href=”<?php echo get_permalink( $post->ID ); ?>”><?php echo get_the_title( $post->ID ); ?></a><br>
<?php endif; ?>
<?php
endfor;
$post = $current_post;
?>

 

<hr>

<?php
global $post;
$current_post = $post; // remember the current post

for($i = 1; $i <= 5; $i++):
$post = get_next_post(); // this uses $post->ID
if ( is_a( $post , ‘WP_Post’ ) ) : ?>
<a href=”<?php echo get_permalink( $post->ID ); ?>”><?php echo get_the_title( $post->ID ); ?></a><br>
<?php endif; ?>
<?php
endfor;
$post = $current_post;
?>

</div>
<!– ————get 5 previous and next posts end—— –>

I have tested the code for https://www.globalsafetyglasses.com