diff --git a/inc/Data/UserPostQueries.php b/inc/Data/UserPostQueries.php index 8f803b93..1c203431 100644 --- a/inc/Data/UserPostQueries.php +++ b/inc/Data/UserPostQueries.php @@ -44,19 +44,20 @@ class UserPostQueries { $posts_count = \count($posts); + // By fetching one extra post bellow and/or above the limit, we know if there are any posts beside the current page. if ($posts_count === $page_size + 2) { $has_extra_prev_post = true; $has_extra_end_post = true; - } elseif ($posts_count === $page_size + 1) { - $has_extra_prev_post = $start_id !== null && $start_id === (int)$posts[0]['id']; - $has_extra_end_post = !$has_extra_prev_post; } else { - $has_extra_prev_post = false; - $has_extra_end_post = false; + /* + * If the id we start fetching from is also the first id fetched from the DB, then we exclude it from + * the results, noting that we fetched 1 more posts than we needed, and it was before the current page. + * Hence, we have no extra post at the end and no next page. + */ + $has_extra_prev_post = $start_id !== null && $start_id === (int)$posts[0]['id']; + $has_extra_end_post = !$has_extra_prev_post && $posts_count > $page_size; } - // Since we fetched one post bellow and/or above the limit, we always know if there are any posts after the current page. - // Get the previous cursor, if any. if ($has_extra_prev_post) { \array_shift($posts);