Support Forum

Welcome to the Support forum.

New Discussion can be made between Monday and Friday (AEST). Comments are never disabled.

Thanks, Elliot

Current status: Solved. . Done
Add flexible layout row with update_field
  • Field looks like this: http://i.imgur.com/dqgOE.png

    Code looks like this:
                $field_key = "field_50aa306490492"; // flexible content field
    //$field_key = "test"; // tried both
    $post_id = "14310";
     
    $value = get_field($field_key, $post_id);
    $value[] = array("test" => "meh", "acf_fc_layout" => "test");
     
    update_field($field_key, $value, $post_id);


    Nothing happens. No errors, either. What am I missing?

    EDIT: WordPress 3.4.2, ACF 3.5.3.1
  • Hi @jessor,

    Your code looks perfect. I'll do some testing and get back to you Perhaps this is a bug with ACF

    Cheers
    Elliot
  • thanks @admin. feels like a bug to me, too. i'm eagerly awaiting the fix :)
  • Hi @admin,
    I'm sorry to bump, but could you please push this a little? ;)
  • Hi @jessor,

    I belive I have fied the problem. Can you edit your core/api.php file and replace the updae_field function with this:
    function update_field($field_key, $value, $post_id = false)
    {
    global $post, $acf;
     
    if(!$post_id)
    {
    $post_id = $post->ID;
    }
     
     
    // allow for option == options
    if( $post_id == "option" )
    {
    $post_id = "options";
    }
     
     
    // is $field_name a name? pre 3.4.0
    if( strpos($field_key, "field_") === false )
    {
    // get field key
    if( is_numeric($post_id) )
    {
    $field_key = get_post_meta($post_id, '_' . $field_key, true);
    }
    elseif( strpos($post_id, 'user_') !== false )
    {
    $temp_post_id = str_replace('user_', '', $post_id);
    $field_key = get_user_meta($temp_post_id, '_' . $field_key, true);
    }
    else
    {
    $field_key = get_option('_' . $post_id . '_' . $field_key);
    }
    }
     
     
    // get field
    $field = $acf->get_acf_field($field_key);
     
     
    // backup if no field was found, save as a text field
    if( !$field )
    {
    $field = array(
    'type' => 'none',
    'name' => $field_key
    );
    }
     
     
    // sub fields? They need formatted data
    if( $field['type'] == 'repeater' )
    {
    $value = acf_convert_field_names_to_keys( $value, $field );
    }
    elseif( $field['type'] == 'flexible_content' )
    {
    if( $field['layouts'] )
    {
    foreach( $field['layouts'] as $layout )
    {
    $value = acf_convert_field_names_to_keys( $value, $layout );
    }
    }
    }
     
     
    // save
    $acf->update_value($post_id, $field, $value);
     
     
    return true;
     
    }


    Let me know how you go with it

    Cheers
    Elliot
  • Jaj, sorry for the late response. That did indeed fix the issue. Thanks!