Support

Account

Home Forums Front-end Issues ACF Load Post – Flexible Content – Not showing up on front end until manual save

Solved

ACF Load Post – Flexible Content – Not showing up on front end until manual save

  • Hello –

    I have a flexible content field. I also have my site setup so that when a new users registers it creates a post for them. I am trying to add some default information into that post, and I am doing so with the following:


    //Prepop Initial Notes
    add_filter(‘acf/load_value/key=field_5eb8736d02e62’, ‘add_starting_flex’, 10, 3);
    function add_starting_flex($value, $post_id, $field) {
    if ($value !== NULL) {
    // $value will only be NULL on a new post
    return $value;
    }

    // add default layouts
    $value = array(
    array(
    ‘acf_fc_layout’ => ‘layout_note_text’,
    ‘field_5eb873ab02e63’ => ‘This is for the General Note only.’
    ),
    array(
    ‘acf_fc_layout’ => ‘layout_video_note’,
    ‘field_5eb8743702e68’ => ‘Starting Video’
    )
    );

    return $value;
    }

    If I view the newly created post on the backend, the layout and default information is loaded perfectly. However, the information will not show on the front end template until I manually save the post.

    Question:
    1) Am I using acf load wrong?
    2) If not, is there a way to auto re-save the post in the same acf load function so I don’t have to manually re-save?

    Thanks

  • To get the fields to load on the front end you need to use an acf/load_field filter without the field key and you need to set the priority low.

    
    add_filter('acf/load_value', 'add_starting_flex', 1, 3);
    

    Then you need to test for the correct field key inside of your filter

    
    if ($field['key'] != 'field_5eb8736d02e62' || $value !== NULL) {
      return $value;
    }
    

    The reason that it is not working for you is that the filter using the field key runs too late and the flex field has a value other then NULL. The only way to bypass the built in ACF filter is by not specifying the field to filter.

  • If I am following you correctly, this is how it should look:

    //Prepop Initial Notes
    add_filter('acf/load_value', 'add_starting_flex', 1, 3);
        function  add_starting_flex($value, $post_id, $field) {
        if ($field['key'] != 'field_5eb8736d02e62' || $value !== NULL) {
          return $value;
        }
    
            // add default layouts
            $value = array(
              array(
                'acf_fc_layout' => 'layout_note_text', 
                'field_5eb873ab02e63' => 'This is for the General Note only.' 
              ),
              array(
                'acf_fc_layout' => 'layout_video_note',
                'field_5eb8743702e68' => 'Starting Video',
              )
            );
             return $value;
       
    } 

    I tried that with no luck. Nothing was passed into the new post now. Did I get that incorrect?

  • I could be wrong, my code I was looking at adds layouts to the array without adding content to any of the fields assuming that they will all be populated with values added as defaults for each sub field.

    What I do is return an array of layout names.

    
    /Prepop Initial Notes
    add_filter('acf/load_value', 'add_starting_flex', 1, 3);
        function  add_starting_flex($value, $post_id, $field) {
        if ($field['key'] != 'field_5eb8736d02e62' || $value !== NULL) {
          return $value;
        }
    
            // add default layouts
            $value = array('layout_note_text', 'layout_video_note');
             return $value;
       
    }
    

    The reason that this works is because at this point in the execution the flex field value is just an array of layout names. When ACF then filters this it populates subfields with default values.

  • Just as a follow-up. Was able to create a new post on a user registration and pre-pop the flexible layout and data with update field. Here is the code I added to my functions.php file for any one else looking:

    
    function create_new_user_posts($user_id){
    // Create new post.
        $post_data = array(
        'post_title' => 'New Post Title',
        'post_type' => 'Custom Post Name',
        'post_status' => 'publish',
        'post_author' => $user_id,
        );
        //Insert the post into the database
        $post_id = wp_insert_post( $post_data );
    
        // Save a flexible content field value.
        $field_key = "field_5eb8736d02e62";
        $value = array(
            array(
            'acf_fc_layout' => 'layout_1',
            'field_5eb873ab02e63' => 'This is what I want the field to say'
            ),
            array(
            'acf_fc_layout' => 'layout_2',
            'field_5eb8743702e68' => 'Starting 2 text'
            )
        );
        update_field( $field_key, $value, $post_id);
    }
    add_action('user_register','create_new_user_posts');
    

    Thank you for your help. Pushed me in the right direction.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.