Tell us what you are trying to do?
When adding new post form I'm looking to insert the author from a populated relationship field in the form which contains the name of the author.
I'm following the guide below which is similar to what I'm after but I don't think I need to create a view as the data that I need is already populated in the relationship field in the same form.
[cred_field field='@agent-entry.parent' value='[wpv-post-id item="@agent-property.parent"]' class='form-control' output='bootstrap' select_text='--- not set ---']
Here's the function:
add_action( 'cred_save_data', 'my_save_data_action', 10, 2 );
function my_save_data_action($post_id, $form_data) {
// if a specific form
if ( $form_data[ 'id' ] == 401950 ) { //Make sure you set the correct ID of your form
$my_post = array(
'ID' => $post_id,
'post_author' => $_POST['@property-entry.parent']
);
// Update the post author ID into the database
wp_update_post( $my_post );
}
}
This kind of works but only if a user is logged in. This form is for guests who do not log in and the author should be inserted from the data in the relationship field which is the agent.
Hello. Thank you for contacting the Toolset support.
Can you please share problem URL where you added the form as well as admin access details and let me see whats going wrong when you submit the post as guest user.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
The first issue was the parent field you need to add it before the submit button so I've moved it the Agent Entry parent field before the submit button as you can see now:
=> hidden link
Then, I've activated the snippet you added and changed the following line of code from:
'post_author' => $_POST['@property-entry.parent']
To:
'post_author' => $_POST['@property-entry_parent']
- The . (dot) is converted to _ (underscore)
Now, the actual code looks like this:
add_action( 'cred_save_data', 'my_save_data_action', 10, 2 );
function my_save_data_action($post_id, $form_data) {
// if a specific form
if ( $form_data[ 'id' ] == 401950 ) { //Make sure you set the correct ID of your for
$my_post = array(
'ID' => $post_id,
'post_author' => $_POST['@property-entry_parent']
);
// Update the post author ID into the database
wp_update_post( $my_post );
}
}
Could you please confirm now it works as expected. You can remove the other parent field from your form that is not required that is added after the submit button.
Hi Minesh,
Thank you but it's still not adding the author to the entry post on form submission.
I need to get the entry agent name to be inserted as the author of the entry post on form submission.
I've adjusted the code as given under within the "Custom Code" section offered by Toolset:
add_action( 'cred_save_data', 'my_save_data_action', 10, 2 );
function my_save_data_action($post_id, $form_data) {
// if a specific form
if ( $form_data[ 'id' ] == 401950 ) { //Make sure you set the correct ID of your for
$author_id = get_post_field ('post_author',$_POST['@agent-entry_parent']);
$my_post = array(
'ID' => $post_id,
'post_author' => $author_id
);
// Update the post author ID into the database
wp_update_post( $my_post );
}
}
I can see it sets the post author successfully now.