Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite-scroll shows load more button even if there are no more posts #282

Closed
katag9k opened this issue Feb 28, 2014 · 7 comments
Closed
Labels
[Feature] Infinite Scroll [Type] Bug When a feature is broken and / or not performing as intended

Comments

@katag9k
Copy link

katag9k commented Feb 28, 2014

Load more button should not be there unless there are actual posts to load. You have to click the button, it attempts to load more posts, there isn't any, then it disappears.

@lancewillett
Copy link
Contributor

Closing as not enough information provided to diagnose. @kategee Feel free to re-open if you have more information.

@katag9k
Copy link
Author

katag9k commented Jul 10, 2014

You just have to have an even number of posts, so if the pagination is set to 5, and there are 10 posts, there will be two pages. On the first page, when you click load more, the next 5 posts appear. Now that all 10 are loaded, the load button still appears. The button should not appear on the second page as there is no more posts to load.

@lancewillett
Copy link
Contributor

Does it happen on any theme? Are you testing with WP trunk and the latest Jetpack version?

@kevinbreed
Copy link

I'm having the same issue on a custom theme. Though I'm using a standalone version of infinite scroll through the infinite-scroll-from-jetpack plugin, the code causing this behaviour is the same as in the latest Jetpack version.

The function is_last_batch() in modules/infinite-scroll/infinity.php checks if the number of fetched posts on a page is smaller than the number of posts to display on a page. So with the example of @kategee, when there are 10 posts in total, and the last page is fetched, the number of fetched posts is 5, just as the number of posts to display on a page. This makes the is_last_batch function return false, while it should return true.

It seems to me that the right way to check for the last 'batch' is to multiply the current page (2 for the last page) with number of posts per page (5) and compare this with the total number of posts for the query (10). To check for the last batch, test whether the current_page*posts_per_page is equal to or greater than the total number of posts.

After some fiddling around with the code, I came to the following solution:

static function is_last_batch() {
    $current_page = self::wp_query()->query_vars['paged']+1;
    $posts_per_page = self::get_settings()->posts_per_page;
    $displayed_posts = $current_page*$posts_per_page;
    $total_posts = intval(self::wp_query()->found_posts)+$posts_per_page;

    return ($displayed_posts >= $total_posts);
}

The only strange thing I encountered is that 'self::wp_query()->found_posts' returns the total number of posts minus the number of posts per page. That's why I've added them again to determine the total number of posts.

@jeherve jeherve reopened this Jul 19, 2015
@samhotchkiss samhotchkiss modified the milestone: Needs Triage Aug 28, 2015
@jeherve jeherve modified the milestones: Not Core Jetpack Team, Needs Triage Feb 15, 2016
@koscoder
Copy link

koscoder commented Aug 5, 2016

Obviously, the function is_last_batch() (version of Jetpack 4.1.1) in modules/infinite-scroll/infinity.php is totaly wrong. To temporary fix it in a theme I used filter 'infinite_scroll_results'.

add_filter('infinite_scroll_results', function($results, $query_args, $wp_query) {
     if ($wp_query->get('paged') == $wp_query->max_num_pages) {
         $results['lastbatch'] = true;
     }
     return $results;
 }, 10, 3);

@ghost
Copy link

ghost commented Sep 20, 2016

@koscoder same here
A temporary fix was to comment out Lines 384-386 in my infinity.php
if ( self::is_last_batch() ) {return;}

@anastis
Copy link

anastis commented Dec 12, 2016

Not sure what this status of this ticket is (or should be), but as of 4.4.2 the issue is pretty-much fixed, and is_last_batch() function looks like this:

	static function is_last_batch() {
		$post_type = get_post_type();
		$entries = wp_count_posts( empty( $post_type ) ? 'post' : $post_type )->publish;
		if ( self::wp_query()->get( 'paged' ) && self::wp_query()->get( 'paged' ) > 1 ) {
			$entries -= self::get_settings()->posts_per_page * self::wp_query()->get( 'paged' );
		}
		return $entries <= self::get_settings()->posts_per_page;
	}

Now, as I said, it's pretty much fixed. While generally it works ok, the Load More Posts button still appears on the second page, when there are exactly two pages.
It seems that self::wp_query()->get( 'paged' ) returns 0-based page numbers, so 1 is the second page, so > 1 only kicks from the third page and after.
Changing > 1 to >= 1 should fix the problem.

@jeherve jeherve modified the milestones: 4.6, Not Core Jetpack Team Jan 11, 2017
@jeherve jeherve modified the milestones: 2/17 - February, 4.7.0 - March 2017 Jan 30, 2017
@samhotchkiss samhotchkiss removed this from the 4.7.0 - March 2017 milestone Feb 3, 2017
leogermani pushed a commit that referenced this issue Aug 12, 2022
Fixes #247
Using is_super_admin() is discouraged in WP 4.8 and later.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Infinite Scroll [Type] Bug When a feature is broken and / or not performing as intended
Projects
None yet
Development

No branches or pull requests

8 participants