Hi Felipe,
Thanks for asking! I'd be happy to help.
When a new post is added through a front-end form, the currently logged-in user who has submitted the form is set as its author.
In case of a visitor (who is not logged in), no post author is set, just as you noted in your video.
( at 1:53 )
From your video, it is not clear exactly what will be the flow of user and post creation through these forms. But, the way I see it, there are two possibilities:
Case 1: Where the actual user will himself/herself fill these two forms:
If the actual user will be submitting both these forms on your website, it would be best to offer the second form to add a new post, only once he/she has logged in. As a result, he/she will be added as the author of any posts that he/she will add in the logged in state.
Case 2: If another user will be submitting these forms, on behalf of other users:
In this case, you'll need to add some custom code.
First, you'll need a function hooked to "cred_success_redirect" ( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect ), which will get the newly created user's ID, when the first form will be submitted and add it to the redirected URL's "user_id" parameter:
// set created user's ID in the URL parameter "user_id"
add_filter('cred_success_redirect', 'custom_user_author_redirect',10,3);
function custom_user_author_redirect($url, $post_id, $form_data) {
if ($form_data['id']==1234) {
$redirect = $url . "?user_id=" . $post_id;
return $redirect;
}
return $url;
}
Note: Replace "1234" with the actual ID of your user form.
Next, you'll add a hidden generic field in your second form for the post, which will get the value of this user's ID, from the URL parameter:
( ref: https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field )
[cred_generic_field type='hidden' field='user_id' urlparam="user_id"]
{
"default":""
}
[/cred_generic_field]
In the last step, you'll need a function that will hook to "cred_save_data" ( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data ), and will update the author of the newly created post, when the second form will be submitted:
( ref: https://codex.wordpress.org/Function_Reference/wp_update_post )
// change author of post when the form submits
add_action( 'cred_save_data', 'custom_user_author_update', 10, 2 );
function custom_user_author_update( $post_id, $form_data ) {
if ($form_data['id']==1234) {
$post = array(
'ID' => $post_id,
'post_author' => $_POST['user_id']
);
wp_update_post( $post );
}
}
Note: Replace "1234" with the actual ID of your post form.
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar