Support

Account

Home Forums General Issues update_field not working?

Solving

update_field not working?

  • I’m trying to check checkbox on all my posts, but no success. This is the code:

    $args = array(
                'post_type' => 'namaste_course',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                );
    
                $post_id = new WP_Query( $args );
    
                $field_key = "CEUAP";
                $value = true;
                update_field( $field_key, $value, $post_id );

    and this is one with a key instead of the name:

                $post_id = "65137";
    
                $field_key = "field_59654068371a3";
                $value = true;
                update_field( $field_key, $value, $post_id );

    Any idea what I am missing here?

  • 
    $args = array(
                'post_type' => 'namaste_course',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                );
    
                $post_query = new WP_Query( $args );
                if ($post_query->have_posts()) {
                  global $post;
                  $field_key = "field_59654068371a3";
                  $value = true;
                  while ($post_query->have_posts()) {
                    $post_query->the_post();
                    update_field( $field_key, $value, $post->ID );
                  }
                  wp_reset_postdata();
                }
    
  • I’m still learning here and just realised that the field_key is for the entire field and not the particular checkbox.

    <div class="acf-field acf-field-checkbox acf-field-59654068371a3" data-name="certifications" data-type="checkbox" data-key="field_59654068371a3">
    		<div class="acf-label"><label for="acf-field_59654068371a3">Certifications</label></div>
    		<div class="acf-input">
    		<input name="acf[field_59654068371a3]" type="hidden">
    <ul class="acf-checkbox-list acf-bl">
    <li><label><input id="acf-field_59654068371a3-NASM" type="checkbox" name="acf[field_59654068371a3][]" value="NASM">NASM</label></li>
    <li><label><input id="acf-field_59654068371a3-ACSM" type="checkbox" name="acf[field_59654068371a3][]" value="ACSM">ACSM</label></li>
    <li><label><input id="acf-field_59654068371a3-NATA-BOC" type="checkbox" name="acf[field_59654068371a3][]" value="NATA-BOC">NATA-BOC</label></li>
    <li><label><input id="acf-field_59654068371a3-ProCert" type="checkbox" name="acf[field_59654068371a3][]" value="ProCert">ProCert</label></li>
    <li><label><input id="acf-field_59654068371a3-AFAA" type="checkbox" name="acf[field_59654068371a3][]" value="AFAA">AFAA</label></li>
    <li><label><input id="acf-field_59654068371a3-NCBTMB" type="checkbox" name="acf[field_59654068371a3][]" value="NCBTMB">NCBTMB</label></li>
    <li><label><input id="acf-field_59654068371a3-CanFitPro" type="checkbox" name="acf[field_59654068371a3][]" value="CanFitPro">CanFitPro</label></li>
    <li><label><input id="acf-field_59654068371a3-PTAGlobal" type="checkbox" name="acf[field_59654068371a3][]" value="PTAGlobal">PTAGlobal</label></li>
    <li><label><input id="acf-field_59654068371a3-CEUAP" type="checkbox" name="acf[field_59654068371a3][]" value="CEUAP">CEUAP</label></li>
    </ul>
    					</div>
    </div>

    I’d like to check the latter one, but using key acf-field_59654068371a3-CEUAP does not work. Any advice? Also, thanks for your reply.

  • A checkbox field stores an array of selected values. You need to get the current value of the field, add the value you want to the array and then update the field.

    But please see my previous comment, your original code was missing a loop for each of the posts you returned with your query.

    
    $args = array(
                'post_type' => 'namaste_course',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                );
    
                $post_query = new WP_Query( $args );
                if ($post_query->have_posts()) {
                  global $post;
                  $field_key = "field_59654068371a3";
                  while ($post_query->have_posts()) {
                    $post_query->the_post();
                    $value = get_field($field_key, $post->ID, false);
                    $value[] = 'CEUAP';
                    update_field( $field_key, $value, $post->ID );
                  }
                  wp_reset_postdata();
                }
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘update_field not working?’ is closed to new replies.