Support

Account

Home Forums ACF PRO update_field firing twice?

Solving

update_field firing twice?

  • Hello,

    Having an issue running the update_field on the front end:

    <?php
    
    // get current count value
    $count = (int) get_field('views');
    
    // increase
    $count++;
    
    // update
    update_field('views', $count);
        
    ?>

    as quoted here https://www.advancedcustomfields.com/resources/update_field/

    The problem is the update seems to happen twice as +2 points are added each time. Also if I $count+10 I might expect the number to increment by 10 but it increases by 20 so I assume update_field is firing twice. How can I stop this from happening?

    I’ve disabled all the plugins and the problem persists.

    Thanks in advance,

    Andrew

  • I don’t think that it’s firing twice unless you’re calling it twice.

    The reason is that even if update_field() does the update twice this code is only called once, unless you are doing it twice

    
    $count = (int) get_field('views');
    
    // increase
    $count++;
    

    So, that means that this portion of code must be running 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?

  • Do you have any AJAX actions that are happening after the initial page load that could be causing your function to fire again?

    I don’t know of anything that will cause a template file to load and run more than once. If this was the case you could see that this was happening by looking that the web pages source code because all of the page HTML would be repeater, you’d have 2 <html> tags in the page.

  • This is just a weird problem.

    A couple of bits of information here…I’ve been doing some digging.

    I installed WP DEBUG and I am only seeing in the Queries tab the value being updated once. I know that it is running twice though. I think the UPDATE sql statement in the logs only once.

    Our site does use AJAX, but it is not firing on page load and that is literally all I am doing is just loading the page. When I refresh, I can see the number go up by 2. Like I explained before, if i open up my database and query the post i am on, if i keep refreshing on that query, I will see it update the number twice.

    I have read some blogs that are saying that single.php can fire twice before of revisions or something so i added this to functions file.

    I also tried moving my function from the functions file to the single.php template. The number still updated twice.

    I even tried converting the function into a shortcode and then instead of calling the function directly, I did a do_shortcode in single.php, but it incremented the number twice.

    I’ve done site wide searches to see if the function is called anywhere else and it is now only in this one template. The function returns the counter to the page and it only outputs once, but that UPDATE statement seems to fire twice.

    I’ll keep playing around with it.

  • Also, i checked the view page source and there is only one <html> tag.

  • How, exactly, is the function get_views_counter() called?

  • At the moment, I have changed it. I made my function into a shortcode.

    So now in the single.php I am doing echo do shortcode.

    I’m getting the same result as before.

    Before, I was just calling the function.

    echo get_views_counter()

    I’ll do a more thorough code review to see if something is coming in late and hitting the page.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘update_field firing twice?’ is closed to new replies.