Support

Account

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?