Support

Account

Home Forums Front-end Issues Use only one submit button with two acf_form saving to specific post ids

Solving

Use only one submit button with two acf_form saving to specific post ids

    • anarita

    • January 13, 2021 at 12:45 am

    Hi,
    I have created a profile page where a user can update a custom post type related to their user id by a post object field.

    $user_id = get_current_user_id();
    $associacao = get_field('associacao', 'user_'.$user_id);
    
    acf_form(array(
            'post_id'       => 'user_'.$user_id,
            'field_groups' => array(617),
            'return' => add_query_arg( 'updated', 'true', get_permalink() )
    ));
    
    acf_form(array(
    	'post_id'       => $associacao->ID,
    	'field_groups' => array(607)
    ));

    Right now it has two submit buttons, but I’d like users to save the data in one go. I tried using the form->false setting but it’s saving just the second form.
    Is there a way to accomplish this?
    Thank you,
    Ana Rita

  • I dont think you can do it in this way, you could either use an ajax approach or a multi step.

  • You can do it without this programing also

  • This could potentially be done, but the process would be painful.

    First you would use the user ID as the id for the form and you would include all of the field and/or groups for the user and the post you want in the form using the acf form settings that let you do this.

    Second you would need to supply your own hidden field for $associacao->ID or you would need to look this up when the the post is saved.

    Next you would need to create an acf/save_post action with a priority of <10 so that it runs before ACF.

    This action should run when the user is being updated if (substr($post_id, 0, 5) == 'user_))`

    You would then need to loop over $_POST[‘acf’], update the fields for the post and then remove them from the array.

    
    if (!emtpy($_POST['acf'])) {
      foreach ($_POST['acf'] as $field_key => $value) {
        
        // you'll need to supply your own logic in this if statement
        if (/* This field key belongs to the post */) {
          // update the post value
          update_field($field_key, $value, $associated_id);
          // remove this field from the array so that ACF does not add it to user
          unset($_POST['acf][$field_key];
        }
    
        
      }
    }
    
Viewing 4 posts - 1 through 4 (of 4 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.