Support

Account

Home Forums General Issues update_field() not working in loop

Helping

update_field() not working in loop

  • Hi guys,

    Breaking my head over this one, why is this code:

    
                if( get_field('field_5b303fe622a15') >= 0 ) {
                    $count = (int) get_field('field_5b303fe622a15');
                    $new_count = $count++;
                    update_field('field_5b303fe622a15', $new_count);
                }
    

    Not working at all inside my if ( have_posts() ) : while ( have_posts() ) : the_post(); loop inside my single.php?

    Thanks!

  • Have you tried to print get_field('field_5b303fe622a15') ? Maybe it’s a subfield ?

    I’ve tested this code on a WP loop and it worked :

    
    $count = (int) get_field('count');
    if ( $count >= 0) {
      $count++;
      update_field('count', $count);
    }
    

    By the way, get_field and update_field can be used with the field’s name instead of the field’s key.

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

You must be logged in to reply to this topic.