Support

Account

Home Forums Add-ons Flexible Content Field Add row and type fields on the Flexible Content Field when I save

Solved

Add row and type fields on the Flexible Content Field when I save

  • Hello.

    I have configured the Flexible Content Field as follows:

    Field Type: Flexible Content
    Field Name: cf_person (Key: field_xxxxxxx)

    Layout
    Name: cf_person_set

    Fields:
    Name: cf_person_text_ja (Key: field_yyyyyy)
    Type: WYSIWYG Editor

    I want to add a new row to the flexible content and set the value of “cf_person_text_ja” to “Dummy Text” when I save a post.
    I want to do this in the functions.php file.

    The following code doesn’t work:

    
    function publish_post_transition_person($post_ID) {
    
      $row = [
        'cf_person_text_ja' => 'Dummy Text',
      ];
    
      update_field('cf_person_set', $row, $post_ID);
    
      return false;
    
    }
    
    add_action( 'save_post', 'publish_post_transition_person' );
    
    
  • Maybe it is solved.

    update_field('cf_person', [], $post_ID); // reset row
    
    $existing_values = get_field('cf_person', $post_ID);
    
    $new_row = array(
      'acf_fc_layout' => "cf_person_set",
      'cf_person_text_ja' => 'Dummy Text'
    );
    
    if ($existing_values) {
      $existing_values[] = $new_row;
    } else {
      $existing_values = array($new_row);
    }
    
    update_field('cf_person', $existing_values, $post_ID);
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.