I hope there are multiple ways to accomplish this, but I suggest you use the following simpler function for this purpose. This is an attempt to grab a post for the current post which excludes it and the previous post:
// A custom function in your functions.php file
function get_related_previous_post($previous = 0) {
global $post; // take current post object
$category_ids = array();
$categories = get_the_category($post->ID); // current post categories
if ($categories) {
foreach ($categories as $individual_category) {
$category_ids[] = $individual_category->term_id;
}
}
$args = array(
'post__not_in' => array($post->ID, $previous), // exclude posts
'posts_per_page' => '1',
'orderby' => 'date',
'order' => 'DESC',
'category__in' => $category_ids // apply same categories
);
$prevprev_id = 0;
$my_query = new WP_Query( $args );
if ($my_query->have_posts()) {
while ($my_query->have_posts()) {
$my_query->the_post();
// Do your stuff
$prevprev_id = $post->ID;
}
}
wp_reset_postdata();
return get_post($prevprev_id);
}
Call the function in your single.php
file like:
if (!$next) {
// Make sure there's a previous post
if ($prev) {
$prevprev = get_related_previous_post($prev->ID);
}
}