• Resolved dave

    (@weboptimizers)


    Hey there,

    First of all, thansk for this awesome plugin.

    I have a very basic repeater field called features. It is attached to a post type software. You can add as many features as you like using the repeater field interface.

    I have created a Form for people to be able to add more fields in the frontend. The idea is that more features can be added in the frontend.

    The form is setup as
    – Action edit current post
    – Save – Toggle custom field “features”
    – Load – nothing

    The problem is when somebody submits a feature in the frontend, it overwrites the previously submitted feature. It should ideally create a new feature. How would I achieve this ?

    • This topic was modified 4 years, 6 months ago by dave.
Viewing 1 replies (of 1 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback. I’m sorry for the late answer, I was quite busy lately with the plugin documentation. Your question is about general ACF development as it is not a feature that is available out-of-the-box.

    First, I would suggest you to un-check the “Features” from the “Post” Action, otherwise it will always replace the features that are already saved in the DB. Next, you should hook on your Post Action submission, using the acfe/form/submit/post/action=my-post action. See documentation.

    This hook will let you work with that new “Features” values submitted by the user, and update the actual meta in the database.

    Here’s a usage example:

    add_action('acfe/form/submit/post/form=my-form', 'my_form_submit', 10, 5);
    function my_form_submit($post_id, $type, $args, $form, $action){
        
        // Get Features input value
        // This is the actual repeater that is submitted by the user
        if(have_rows('features')):
            while(have_rows('features')): the_row();
                
                // Get sub fields
                $sub_field_1 = get_sub_field('sub_field_1');
                $sub_field_2 = get_sub_field('sub_field_2');
                
                // Generate a row for the update
                $row = array(
                    'sub_field_1' => $sub_field_1,
                    'sub_field_2' => $sub_field_2,
                );
                
                // Add a new row in the DB in the currently updated post
                // Note: you must use field key here
                add_row('field_5f4cff15864aa', $row, $post_id);
            
            endwhile;
        endif;
        
    }
    

    Video example: https://i.imgur.com/QlHvs1W.mp4

    Hope it helps!

    Regards.

Viewing 1 replies (of 1 total)
  • The topic ‘Create a new repeater field value instead of overwriting the old one.’ is closed to new replies.