How to set the newly created user as author of a newly created post

Created by Phil Kurth, Modified on Mon, 01 Aug 2022 at 02:12 PM by Phil Kurth

When using a form to create both a user and post, you may choose to set the new user as the post author. Advanced Forms Pro handles user creation first and appends the newly created user ID to the AF()->submission array under the 'user' key. It is then available for use in subsequent hooks/filters as per the following example:

add_filter( 'af/form/editing/post_data', function ( $post_data, $form, $args ) {
  if ( $form['key'] === 'FORM_KEY' ) {
    $post_data['post_author'] = AF()->submission['user'];
  }
  
  return $post_data;
}, 10, 3 );

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article