Home › Forums › ACF PRO › update_field firing twice? › Reply To: update_field firing twice?
I seem to be having the same kind of issue. I have a function in my functions.php for my theme.
The function looks like this:
function get_views_counter() {
global $post;
$post_id_for_counter = $post->ID;
$current_view_counter = (int) get_field('view_counter', $post_id_for_counter);
if($current_view_counter >= 0) {
$updated_counter = $current_view_counter + 1;
update_field('view_counter', $updated_counter, $post_id_for_counter);
}
$new_view_counter = (int) get_field('view_counter', $post_id_for_counter);
$number_formated_int = (int) number_format($new_view_counter);
$output = $new_view_counter . " views";
return $output;
}
The way I am testing it is I have the database open and i do a query so i can see the value of the view_counter field right in the database. I refresh that query while I am loading the page.
I go to the browser, open an article from the site where this function is fired, and if i echo something out from this function I only see it output once.
Initially when the page is loaded, I see in the database the number will increment once. Then after a few seconds, it increments again. If I sit there and hit refresh I will see it increment by 2 every time.
I had read of some issues with single.php loading twice. I am in single.php so trying to figure what it causing my function to fire twice.
Could it be that it is in the functions file?
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!
2022 was a big year for Advanced Custom Fields, with more capabilities, a new generation of ACF Blocks, a refreshed UI, and a new home. Our year in review post looks at advancements we’ve made and offers a glimpse of the future.
— Advanced Custom Fields (@wp_acf) January 6, 2023
https://t.co/HahJUCcyH4
© 2023 Advanced Custom Fields.
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.