Support

Account

Home Forums Backend Issues (wp-admin) update_sub_field doing nothing, with or without keys.

Solving

update_sub_field doing nothing, with or without keys.

  • Howdy,

    I’m trying to combine ACF with an interface to make everything editable for a post in one place, in a clean look that can’t be achieved on a normal page. In order to do this, I need to update a post by AJAX, which so far works out really well – I can pull all the information I’d need, and grab it with ACF syntax. That is, until it gets to actually updating the content.

    When I try to update said content, absolutely nothing happens. No debug.log reports, no PHP errors, nothing.

    Here’s a simple version of what I’m doing (that doesn’t work):

    
    if( have_rows('content_type', $post_id) ) :
          while( have_rows('content_type', $post_id) ) : the_row();
            if( get_row_index() == 2 ) :
              $this_row_layout = get_row_layout();
              if( have_rows('label') ) :
                while( have_rows('label') ) : the_row();
                $this_layout_key = $this_layout[$this_row_layout]['key'];
                // now we have the key of the flexible content layout
                $label_group_key = $this_layout[$this_row_layout][1]['key'];
                // now we have the key of the 'label' group
                $label = $this_layout[$this_row_layout][1]['sub_fields'][0]['key'];
                // now we have the key of the 'label' field
                $subtitle = $this_layout[$this_row_layout][1]['sub_fields'][1]['key'];
                // now we have the key of the 'subtitle' field
                print_r(array($this_layout_key,$this_count,$label_group_key,$label));
                  update_sub_field( array($this_layout_key,$this_count,$label_group_key,1,$label), $the_content['label'], $post_id);
                  update_sub_field( array($this_layout_key,$this_count,$label_group_key,1,$subititle), $the_content['subtitle']);
                endwhile;
              endif;
              if( have_rows('css') ) :
                while( have_rows('css') ) : the_row();
                // here, I'm trying to do this without all the complex field name stuff, and without specifying post ID.
                  update_sub_field( 'classes', $the_content['classes']);
                  update_sub_field( 'id', $the_content['style_id']);
                endwhile;
              endif;
            endif;
          endwhile;
        endif;

    Yes, those variables to get keys do work, I have them echo into console and they are the correct keys.

    Can anyone help with even just debugging? I have scoured the forums for similar issues, and none of them have helped.

  • I’m not sure it’ll help you exactly, but figured I’d chime in with something I spent a while troubleshooting for update_sub_field(), for a custom admin interface I was working on. I wanted to update multiple fields within a Group, within a Repeater. Here’s my code:

    $values = array(
    	//field1 => value
    	'field_5aac179f96382' => 'emailed',
    	//field2 => value
    	'field_5aac164496381' => 'TEST5678!'
    );
    
    //update_sub_field( array(repeater_name, row_id, group_name), $values, post_id )
    update_sub_field( array('field_5a71dfd39fac7', 1, 'field_5a7209ec9facd'), $values, 116 )

    What I did to troubleshoot in my situation, was create a new WordPress page and outputted the above code on that page. Then, I know when I visit that page in the frontend, that my field “should” be updated on my post and if it doesn’t then there’s something wrong with my code.

  • @graphix,

    Thanks for some pointers, but unfortunately this isn’t working for me. I’m curious if it’s because I’m using a Flexible Content Type vs a Repeater?

    I’m able to get the saving to work via WordPress’ update_post_meta function, just using

    
        $label = 'content_type_'.$this_count.'_label_label';
        $subtitle = 'content_type_'.$this_count.'_label_subtitle';
        update_post_meta( $post_id, $label, $the_content['label']);
    

    However, I worry that when new content types and repeaters are added in this manner, they may not be ‘registered’ with ACF. I feel I’m missing something important in the structure of my code, as it seems to work for everyone but me!

    From some fiddling, get_fields actually shows me that there is a ‘new’ field added in for each of the groups I updated, e.g.

    
    //before updating label in group label
    content_type_label_label => ''
    

    becomes

    
    //after updating label in group label
    content_type_label_0_label => 'test',
    content_type_label_label => ''
    

    Or something to that effect.

  • Can you post a screenshot of structure of your ACF Flexible Content field group page (i.e. the interface where you created your Flexible Content field)?

  • Here’s a big ol screenshot of my baby

  • Shot in the dark, as this is pretty much what you had, but give something like this a try:

    //Perhaps use the field_key here
    if( have_rows('content_type', $post_id) ) :
        while( have_rows('content_type', $post_id) ) : the_row();
    
    		if(get_row_layout() == 'text_field'): //The Name of our Flexible Content Row
    			
    			//Perhaps use the field_key here
    			if( have_rows('label') ) : 
    				while( have_rows('label') ) : the_row();
    					
    					//Perhaps use the field_key here
    					update_sub_field('subtitle', "This is a new Subtitle!");
    					
    				endwhile;
    			endif;
    
    		endif;
    
    	endwhile;
    endif;
  • @graphix

    Thanks again, Using this I can verify, by adding in an echo, that it gets through to the label group, using keys I’ve snagged from another array, but when it comes to update_sub_field, whether using the name or field key, it doesn’t work. I’m going to continue checking logs, but nothing is popping up…

    EDIT:

    I tried echoing the result of update_sub_field, and it seems only the first one (for ‘label’) does anything – it returns true, though it doesn’t actually update anything! A bit odd that it both does nothing, and doesn’t continue to the next update_sub_field, is it not?

  • I’m looking at this thread: https://support.advancedcustomfields.com/forums/topic/issues-with-add_rows-and-flexible-content/

    According to this, as of 2016, add_row on flexible fields weren’t working. Instead, they used update_field, which added new rows? Anyways, I wonder if it has something to do with flexible fields…

    Additionally, I have been thinking that I may be hooking incorrectly in my functions.

    First, the full code for this specific piece:

    
    <?php
    /**
     *
     *   Update the content of a field.
     *
     */
    
    class POB_Save_Settings {
    
      public static function init() {
    
          add_action( 'wp_ajax_nopriv_save_settings_fields', array( 'POB_Save_Settings', 'save_settings_fields' ) );
          add_action( 'wp_ajax_save_settings_fields', array( 'POB_Save_Settings', 'save_settings_fields' ) );
    
      }
    
      public function update_static_content($post_id, $this_count, $the_content){
        //print_r(get_field_objects($post_id));
        $all_fields = get_field_objects($post_id);
        $this_field = $all_fields['content_type']['layouts'];
        $this_row_layout = $all_fields['content_type']['value'][$this_count]['acf_fc_layout'];
        $this_layout = array();
        foreach($this_field as $layout => $key){
          $this_layout[$key['name']] = $key['sub_fields'];
          $this_layout[$key['name']]['key'] = $layout;
        }
    
        $this_layout_key = $this_layout[$this_row_layout]['key'];
        $label_group_key = $this_layout[$this_row_layout][1]['key'];
    
        if( have_rows('content_type', $post_id) ) :
          while( have_rows('content_type', $post_id) ) : the_row();
          echo 'content_type while' . '<br>';
          echo get_row_index() . '<br>';
          echo $this_count+1 . '<br>';
            if( get_row_layout() == $this_row_layout && get_row_index() == $this_count+1 ) :
              echo 'true' . '<br>';
              update_post_meta( $post_id, 'content_type_'.$this_count.'_title', $the_content['title']);
              if( have_rows($label_group_key) ) :
                while( have_rows($label_group_key) ) : the_row();
                /*$values = array(
                  $this_layout[$this_row_layout][1]['sub_fields'][0]['key'] => $the_content['label'],
                  $this_layout[$this_row_layout][1]['sub_fields'][1]['key'] => $the_content['subtitle']
                );
                print_r($this_field);*/
                $label = 'content_type_'.$this_count.'_label_label';//$this_layout[$this_row_layout][1]['sub_fields'][0]['key'];
                $subtitle = $this_layout[$this_row_layout][1]['sub_fields'][1]['key'];//'content_type_'.$this_count.'_label_subtitle';
                echo update_sub_field( 'label', $the_content['label']);
                echo update_sub_field( 'subtitle', $the_content['subtitle']);
                //update_sub_field( array($this_layout_key, 1, $label_group_key), $values, $post_id );
                endwhile;
              endif;
              if( have_rows('css') ) :
                while( have_rows('css') ) : the_row();
                $cssclass = 'content_type_'.$this_count.'_css_classes';
                $cssid = 'content_type_'.$this_count.'_css_id';
                update_post_meta( $post_id, $cssclass, $the_content['classes']);
                update_post_meta( $post_id, $cssid, $the_content['style_id']);
                endwhile;
              endif;
            endif;
          endwhile;
        endif;
      }
    
      public static function save_settings_fields(){
        if(isset($_POST['to_save'])){
          $post_info = $_POST['details'];
          $post_id = $post_info['postid'];
          $this_count = $post_info['index'];
          $saved_content = $_POST['to_save'];
          $all_content = array();
          foreach($saved_content as $array){
            $all_content[$array['name']] = $array['value'];
          }
          print_r($all_content);
        } else {
          wp_die($_POST.' not set!');
        }
        //wp_send_json($response);
        $foobar = new POB_Save_Settings;
        $isdone = $foobar->update_static_content($post_id, $this_count, $all_content);
        if ( is_wp_error( $isdone ) ) {
            wp_send_json_error( $isdone );
        } else {
          echo 'yay!';
          wp_die();
        }
      }
    
    }
    

    So I’m using AJAX to send information to this. In my plugin’s functions.php, we got:

    
    define('PLUGIN_DIR_PATH', plugin_dir_path(__FILE__));
    
    require_once PLUGIN_DIR_PATH . 'admin/classes/pob-category-walker.php';
    include_once( plugin_dir_path( __DIR__ ) . 'advanced-custom-fields-pro/acf.php' );
    include_once PLUGIN_DIR_PATH . 'admin/pob-remote-post-class.php';
    include_once PLUGIN_DIR_PATH . 'admin/views/panes/pane_layout_radio_buttons.php';
    include_once PLUGIN_DIR_PATH . 'admin/classes/pob-save-settings.php';
    include_once PLUGIN_DIR_PATH . 'admin/classes/pob-create-field.php';
    POB_Remote_Post::get_instance();
    Radio_Buttons_Layout::init();
    POB_Save_Settings::init();
    POB_Create_Field::init();
    
    function update_acf_settings_path( $path ) {
        $path = plugin_dir_path( __DIR__ ) . 'advanced-custom-fields-pro/';
        return $path;
    }
    add_filter( 'acf/settings/path', 'update_acf_settings_path' );
    function update_acf_settings_dir( $dir ) {
        $dir = plugin_dir_url( __DIR__ ) . 'advanced-custom-fields-pro/';
        return $dir;
    }
    add_filter( 'acf/settings/dir', 'update_acf_settings_dir' );
    
    do_action('acf/input/admin_head'); // Add ACF admin head hooks
    do_action('acf/input/admin_enqueue_scripts'); // Add ACF scripts
    add_action( 'admin_init', 'add_acf_variables' );
    function add_acf_variables() {
        acf_form_head();
    }
    

    With the last pieces (after init of those classes) being added recently, making shots in the dark.

    Could someone perhaps check that I’m hooking properly to update content? Or if I’m missing something there?

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

You must be logged in to reply to this topic.