WordPress.org

Ready to get started?Download WordPress

Forums

Jetpack by WordPress.com
[resolved] Fix for Popular Posts showing only 9-10 posts (7 posts)

  1. Christoph Nahr
    Member
    Posted 1 year ago #

    Jetpack's Popular Posts widget has a known bug where only 9-10 posts are shown, regardless of how high the desired number is set or how many posts were viewed during the last two days. This was e.g. discussed here but incorrectly marked as "resolved". The workaround proposed there shouldn't do anything. Below follows the explanation of what I found, and how I fixed it on my installation.

    First, the stats module's top views always include the front page. But Jetpack's widget doesn't show that if it's the usual list of recent posts rather than an edited page. So you'll have to increment your desired count by one to begin with: specify 11 to show 10 entries in the widget.

    Second, there's a bona fide bug in this PHP file:
    wp-content\plugins\jetpack\modules\widgets\top-posts.php

    Line 225 in function get_by_views($count) hard-codes the total number of popular entries fetched from the stats module to 10, rather than passing the specified $count:
    $post_view_posts = stats_get_csv( 'postviews', array( 'days' => 2, 'limit' => 10 ) );

    This is the fixed line, and with it the Popular Posts widget works correctly on my installation, modulo the front page issue described above:
    $post_view_posts = stats_get_csv( 'postviews', array( 'days' => 2, 'limit' => $count ) )

    http://wordpress.org/extend/plugins/jetpack/

  2. Jeremy Herve
    Happiness Engineer
    Plugin Author

    Posted 1 year ago #

    First, the stats module's top views always include the front page. But Jetpack's widget doesn't show that if it's the usual list of recent posts rather than an edited page. So you'll have to increment your desired count by one to begin with: specify 11 to show 10 entries in the widget.

    Your front page can indeed be among the most viewed pages on your site. That's why the widget settings specify a "Maximum number of posts to show", and not an exact number. I agree that this can be a bit confusing though.

    Line 225 in function get_by_views($count) hard-codes the total number of popular entries fetched from the stats module to 10, rather than passing the specified $count:
    $post_view_posts = stats_get_csv( 'postviews', array( 'days' => 2, 'limit' => 10 ) );

    This line allows the plugin to get a list of posts, and we always query for 10 posts (the maximum that can be displayed in the Top Posts and Pages widget. Once we've gathered all 10 posts, we limit the output to the $count you specified in your widget settings, on line 235:

    return $this->get_posts( $post_view_ids, $count );

  3. Christoph Nahr
    Member
    Posted 1 year ago #

    This line allows the plugin to get a list of posts, and we always query for 10 posts (the maximum that can be displayed in the Top Posts and Pages widget.

    I hate to correct you, but that is not true. With my fixed PHP file, I just entered 12 as the desired number of Top Posts & Pages, and the resulting blog page showed 11 entries. (The usual front page was missing.)

    Moreover, before fixing the PHP file as described, I could not achieve more than 9 entries regardless of the number I entered. Now I can get 10 displayed entries by requesting 11, again due to the silently omitted front page.

  4. George Stephanis
    Automattic Lead Code Monkey
    Plugin Author

    Posted 1 year ago #

    Hiya, Christoph.

    Regarding the function here:

    http://plugins.trac.wordpress.org/browser/jetpack/trunk/modules/widgets/top-posts.php#L223

    You're confusing the theoretical maximum (which indeed is unlimited) with the maximum that the code as written was intended to allow (up to ten).

    The number is hard-coded as ten for performance reasons to allow the stats_get_csv() function here:

    http://plugins.trac.wordpress.org/browser/jetpack/trunk/modules/stats.php#L1026

    to cache the result, and share it between multiple widgets with assorted counts. So ten items are always returned, then they are filtered for post IDs, which is where the 'up to' comes into play. It could be ten, it could just as easily be eight or nine depending on which of the returned entries map to posts here

    Can you elaborate on why you need it to be exactly ten items, and a normal list of 'up to ten' is inadequate? If it's a reasonable and common use case, I'd be happy to consider making the change in trunk, or at least adding a filter to make it overrideable without modifying core. :)

  5. Christoph Nahr
    Member
    Posted 1 year ago #

    You're confusing the theoretical maximum (which indeed is unlimited) with the maximum that the code as written was intended to allow (up to ten).

    Well, I had just patched out that limit so I wasn't sure what Jeremy was referring to.

    The number is hard-coded as ten for performance reasons to allow the stats_get_csv() function here:

    Thanks for the explanation. As for why I want exactly ten items, that's just for symmetry with the ten recent posts and because it's a nice round number! So no, I don't have a strong reason for more items.

    But here's why I considered this hard-coded limit a bug: The widget itself allows entering numbers up to 20, not 10. That's a very confusing discrepancy, and if you say the hardcoded limit should remain at 10 for caching then IMO you should definitely fix the UI code so that its limit is the same as the internal one, i.e. 10.

  6. George Stephanis
    Automattic Lead Code Monkey
    Plugin Author

    Posted 1 year ago #

    Sounds like a plan, I'll try and get a patch pushed through to correct it down to 10 in Jetpack shortly. :) I think it may be 20 because in WordPress.com sites where the widget doesn't need to make the API calls there's not the same lag time or the same limit -- but I've not checked that so I can't be certain.

    I'll try to post back when there a conclusion to update you on. If you don't hear back in a week tops, feel free to shoot off a signal flare. :)

  7. George Stephanis
    Automattic Lead Code Monkey
    Plugin Author

    Posted 1 year ago #

    Actually it looks like the form fields are set to accept a max of ten:

    http://plugins.trac.wordpress.org/browser/jetpack/tags/2.2.5/modules/widgets/top-posts.php#L77

    however some of the validation functions were missed, so values up to 20 could slip through. I'm correcting it now.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic