Home Forums Pre-purchase Questions Post Views Counter


Post Views Counter

  • Hi,

    is it possible to create a custom field for page views?
    Everytime when a post is loaded the view counter should increase and will be saved in the custom field.

    best regards,

  • In the template file, get the field, increment it and then save it

    $count = get_field('counter_field);
  • Thanks John, but your code didn’t work for me..

    Managed to find something that did work, thought I’d add it here in case anyone else comes looking..

    // Get the current value.
    $count = (int) get_field('counter_field');
    // Increase it.
    // Update with new value.
    update_field('counter_field', $count);
  • but in which php file do i insert this code?

  • In the single post template or the template that shows the single post for whatever post type you want to count.

  • I inserted in single.php but the counter is not working, see my prints, am I missing somewhere?


  • My version in Header.php

    $count = get_field('views');			  
    update_field('views', number_format($count,0,",","."));
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.