Home › Forums › Pre-purchase Questions › 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,
Sascha
In the template file, get the field, increment it and then save it
$count = get_field('counter_field);
$count++;
update_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.
$count++;
// Update with new value.
update_field('counter_field', $count);
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
<?php
if(is_singular('post')){
$count = get_field('views');
$count++;
update_field('views', number_format($count,0,",","."));
}
?>
Thank you for sharing. The website used to be another system. I want to manually modify the fields and increase the original views. Which table in the database, how to manually modify it, and also ask how to count the views on all post.
The topic ‘Post Views Counter’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.