Skip to content

WP-PostViews Issue: Views Count Restriction Not Working #61

Closed

Description

@rashedexe

WP-PostViews Issue: View Count Restriction Not Working

In WP-PostViews settings, “Count Views From” is set to “Registered Users Only”, but the view count is still increasing when the site is accessed by non-logged-in/guest users.

Please check and fix this so that when Registered Users Only is selected, only logged-in users’ views are counted. Guest/visitor views should not be added to the post view count.

https://prnt.sc/fax4fmuZgb4Q

Additional Issue: LiteSpeed Cache

I’ve been facing another issue for a long time with the WP PostViews plugin.

Also, I’ve noticed that LiteSpeed Cache can interfere with the view-counting system, so views are sometimes not counted correctly because of caching.

Please check whether the plugin is fully compatible with LiteSpeed Cache and make sure the view count is recorded accurately without being affected by page caching.

Activity

  1. lesterchan commented on Aug 22, 2026

    @lesterchan
    Owner

    @claude Is this a bug?

  2. claude commented on Aug 22, 2026

    @claude

    Claude finished @lesterchan's task in 1m 13s —— View job


    Todo List

    • Gather context (read issue, relevant code)
    • Investigate "Count Views From: Registered Users Only" logic
    • Investigate LiteSpeed Cache compatibility concern
    • Post analysis/answer

    Is this a bug?

    Yes — the "Registered Users Only" restriction is genuinely broken when a page cache (LiteSpeed Cache, or anything else that sets WP_CACHE) is in play, and the two parts of the report are actually one bug.

    Where the check lives: WP_PostViews_Counter::should_count() (includes/class-wp-postviews-counter.php:91-141) correctly implements all three modes — for count = 2 (registered only) it only returns true when get_current_user_id() > 0. When there's no page cache, process() runs this check fresh on every single wp_head, so it works exactly as configured.

    Where it breaks: with a page cache, counting goes through the AJAX path instead (using_ajax() at line 169). Here's the sequence:

    1. enqueue() (line 236) calls should_count() once, at the moment the page is rendered and cached, and only then decides whether to output the counting script (wp-postviews-cache.js) into the HTML.
    2. That HTML — including the presence or absence of the script — gets baked into the cache and served identically to every subsequent visitor, regardless of who they are.
    3. When the script does run and posts to admin-ajax.php, ajax_increment() (line 279) hands off straight to record() (line 314), which never calls should_count() again — it only validates that the post exists and is publicly viewable, then increments unconditionally.

    So the "registered users only" gate is only ever evaluated against whichever visitor happened to trigger that particular cache generation — not against the visitor whose click is actually being counted. If a logged-in user's visit populates the cache, the script gets embedded, and every guest who then loads that cached copy will trigger a real, fresh admin-ajax.php request that record() accepts unconditionally — hence guest views incrementing the count under "Registered Users Only." (The reverse also happens: if a guest populates the cache first, no registered user's view gets counted until the cache is purged.)

    This isn't a LiteSpeed-specific incompatibility — LiteSpeed Cache is explicitly a supported target for the AJAX counting path (see README.md:193-209, should_count()/using_ajax() design). It's that the eligibility check for the "who counts" setting isn't re-verified at record time, only at cache-generation time, which any page-caching plugin (LiteSpeed, WP Rocket, etc.) will expose.

    Fix direction (not implemented here, since the request was to confirm whether this is a bug): ajax_increment() should re-run the equivalent of should_count()'s user-eligibility check against the actual requester before calling record(), rather than relying solely on the enqueue-time decision baked into the cached HTML.

    No code changes were made — this was an investigation to answer "is this a bug," not an implementation request. Happy to implement the fix if wanted.

  3. lesterchan commented on Aug 22, 2026

    @lesterchan
    Owner

    @rashedexe Thanks for the clear report.

    The bug was that the "Count Views From" setting was only checked while the page renders. On a cached site the counting happens through a separate AJAX call instead, and that endpoint never asked the setting. So a guest hitting a cached page was counted even under "Registered Users Only".

    The fixed is the endpoint (and the REST route) now check the setting with the actual visitor's login state, so guests no longer count under Registered Users Only and logged-in users still do. Bot exclusion now applies on that path too. Will be in the next release.

    On LiteSpeed Cache: keep "Use AJAX to Update Views" enabled in the plugin settings. That is what makes views count on cached pages at all. LiteSpeed does not cache admin-ajax.php by default, so no extra config is needed there. Note that if a cached page sits longer than about a day, the security token baked into it expires and views stop counting until the page is re-cached; that is a limitation of how WordPress nonces work with long-lived page caches.

    In the mean time you can download https://github.com/lesterchan/wp-postviews/archive/refs/heads/master.zip and overwrite

    • includes/class-wp-postviews-counter.php
    • includes/class-wp-postviews-api.php
  4. rashedexe commented on Aug 22, 2026

    @rashedexe
    Author

    Even with the LiteSpeed Cache plugin completely deactivated, guests can still see/count views when “Count Views From: Registered Users Only” is enabled.

    So, is this issue also covered by the fix you mentioned?

    Also, do you have an estimated date for when the official release with this fix will be available?

  5. lesterchan commented on Aug 23, 2026

    @lesterchan
    Owner

    Yes. I will bundle more more fixes, probably mid Sept

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions