Pagination go to first page if i’m on last post

Here is one idea for the previous post circle:

$next_post = get_next_post();
$prev_post = get_previous_post_circle();

where we have defined this function:

function get_previous_post_circle($in_same_cat = false, $excluded_categories=""){
    $prev_post = get_previous_post($in_same_cat,$excluded_categories);
    if($prev_post){
        return $prev_post;
    }else{
        add_filter('get_previous_post_where','custom_get_previous_post_where');
        $prev_post = get_previous_post($in_same_cat,$excluded_categories);
        remove_filter('get_previous_post_where','custom_get_previous_post_where');
        if($prev_post){
            return $prev_post;
        }else{
            return '';
        }
    }
}

and the filter function to remove the specific date comparison:

function custom_get_previous_post_where($where){
    $where=" WHERE ".implode(" AND ",array_slice(explode("AND",$where),1));
    return $where;
}

The aim is to emulate the get_previous_post() function with the same input parameters, but you could of course play with get_post() in your code instead to create the closed loop.

Leave a Comment