After WordPress enables CDN static page caching, when an article is visited, the request is not sent to the origin server. Instead, the page cached by the CDN is returned, so the article view count will not be incremented or otherwise updated.

To solve this problem, you can use Ajax to send an asynchronous request to the server, and then increment the article view count or perform other operations.

Important reminder:

This method is only applicable to blogs using the WP-PostViews plugin or the same fields as WP-PostViews. If your article statistics fields are different, you can modify the SQL statement below as needed to achieve the desired effect.

Solution

First, create a PHP file in the WordPress root directory, name it views.php, and then write the following code into the file:

<?php require('wp-blog-header.php'); ?>
<?php
  $ajaxHead =  $_SERVER['HTTP_X_REQUESTED_WITH'];
  if (strtoupper($ajaxHead) == strtoupper("XMLHttpRequest")) {
    $post_id = $_POST['post_id'];
    echo $post_id;
    $sql = "update wp_postmeta set meta_value = meta_value + 1 where post_id = '${post_id}' AND meta_key = 'views';";
    $wpdb->query($sql);
  } else {
    header("HTTP/2 404 Not Found");
    header("Status: 404 Not Found");
    exit;
  }
?>

In the code above, an Ajax request header check is implemented to prevent malicious requests and verify whether the request is an XMLHttpRequest request. If it is not, a 404 status is returned.

Next, insert the following JS code into the theme's page.php file:

<script>
  $(function() {
    $.post("/views.php", { post_id: <?php the_ID(); ?> }, function() {});
  });
</script>

This completely solves the problem of WordPress article view counts failing to increase after CDN caching is enabled.