START NOW

How to Create Page Views Counter using ACF

June 13, 2021

A visitor counter keeps track of how often your website or website's page is accessed. This may help your marketing strategy on your product pages or content.
While the visual output of the user data generally only serves a representational purpose, many website operators also use the discovered visitor numbers for web analytics.

To create a page views counter, you will need a script that will be triggered every time a user opens the page containing the page views counter script. You will also need a database to store that data.

Creating Page Counter in WordPress posts, pages, custom post types, and product pages.

I will be doing a page counter for a client machinebluebook.com  for their directory and product pages. With WordPress, page metadata can be stored in wp_postmeta table. Since we are using Advanced Custom Fields ACF we can use this to store our page views counter data for each post, product, and custom post type.

Steps to create page counter using ACF

  • Create a New Custom Field Group
    WordPress Dashboard > Custom Fields > Add New
  • And new field and label it. In my case, I named it Page Views.
    Custom Field Page Views
  • Make sure to set the location to the desired group. If you have multiple post types, make sure you set rules to "or".
    Advance Custom Field Page Views Counter Field Rules
  • Insert this code on your theme template.
// Get the current value.
$count = (int) get_field('page_views');
// Increase it.
$count++;
// Update with new value.
update_field('page_views', $count);
  • To display it on your template, just echo $count
echo $count;

Other usages for Page Views Counter data.

Now that you have this data for your custom post types and products, you can use it to sort queries based on page views or use this data to improve your marketing strategy.

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign up for newsletter
You have reached the bottom. The only way is to go up!
power-switchcross