{"id":19037535,"date":"2026-10-06T09:13:50","date_gmt":"2026-10-06T09:13:50","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/ajax-loading-news-update-view\/"},"modified":"2026-10-06T09:13:50","modified_gmt":"2026-10-06T09:13:50","slug":"ajax-loading-news-update-view","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/ajax-loading-news-update-view\/","title":{"rendered":"Ajax Loading News &amp; Update View"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hello,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I am using <strong>WP Statistics 14.16.13<\/strong> on a WordPress website with a custom news theme.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I have implemented an infinite scroll for single news posts. The first post is loaded normally, so WP Statistics correctly records its view.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All subsequent posts are loaded via AJAX without reloading the page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The flow is:<\/p>\n\n\n\n<ol start=\"1\">\n<li>User opens:<br \/><code>\/news\/post-1\/<\/code><\/li>\n\n\n\n<li>WP Statistics correctly records a view for <code>post-1<\/code>.<\/li>\n\n\n\n<li>User scrolls down and <code>post-2<\/code> is loaded via AJAX.<\/li>\n\n\n\n<li>Then <code>post-3<\/code>, <code>post-4<\/code>, etc. are loaded via AJAX.<\/li>\n\n\n\n<li>When a new post becomes the currently viewed post, I also update the browser URL using <code>history.replaceState()<\/code>:<br \/><code>\/news\/post-2\/<\/code><br \/><code>\/news\/post-3\/<\/code><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The problem is that WP Statistics continues to record views for the initially opened page, while the AJAX-loaded posts do not receive their own view count.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I display the post statistics using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp_statistics_pages('total', null, get_the_ID())<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For AJAX-loaded posts, <code>get_the_ID()<\/code> is correct and returns the ID of the specific post being rendered.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each AJAX-loaded post also contains its own URL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div\n    class=\"singleNews-block\"\n    data-url=\"https:\/\/example.com\/news\/post-2\/\"\n    data-title=\"Post 2\"\n&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The browser URL is changed with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>history.replaceState(\n    { url: url },\n    '',\n    url\n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I also tried using the WP Statistics REST endpoint:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/wp-json\/wp-statistics\/v2\/hit<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">However, the request does not associate the hit with the required <code>post_id<\/code>. Instead, the view continues to be attributed to the initially opened post.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I need the following behavior:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Initial post #1\n\u2192 WP Statistics +1 for post ID #1\n\nAJAX-loaded post #2\n\u2192 when it becomes the currently viewed post\n\u2192 WP Statistics +1 for post ID #2\n\nAJAX-loaded post #3\n\u2192 when it becomes the currently viewed post\n\u2192 WP Statistics +1 for post ID #3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The page itself is never reloaded.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Is there an official PHP API, REST API, hook, or internal method in <strong>WP Statistics 14.16.13<\/strong> that can be used to manually register a view for a <strong>specific WordPress post ID<\/strong> during AJAX\/infinite-scroll navigation?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I specifically need a method where I can provide the <code>post_id<\/code> and record the view for that particular post, rather than having WP Statistics determine the post from the current HTTP request URL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Could you please provide an example implementation compatible with WP Statistics 14.16.13?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"template":"","class_list":["post-19037535","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/19037535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/19037535\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=19037535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}