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

  • 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];
        }
    
        
      }
    }
    
  • Thank you for all your detailed input. I’ll see if it’s worth the effort (not all steps are clear to me now and I’m unfortunately pressed for time), and if so, I’ll post it here.

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

You must be logged in to reply to this topic.