I am currently using this code snippet I found on Toolset forums:
add_action('cred_save_data', 'func_set_custom_author',10,2);
function func_set_custom_author($post_id, $form_data){
// if a specific form
if ($form_data['id']==66944){
$my_post = array(
'ID' => $post_id,
'post_author' => 51
);
// Update the post into the database
wp_update_post( $my_post );
}
}
However I would like to only do this if the user is a guest, is this possible?
So if they are logged in, it assigns the post to them but if they are a guest then it assigns them to post_author ID 51.
As I need to set something otherwise it doesn't set anything and then when you go to edit it it just picks the first user in the list as the author when you go to save.
Hello. Thank you for contacting the Toolset support.
As I understand, basically, what you want is, if user is loggedin, the author will be automatically set but if user submit the form as guest user, you want to assign specific author to that post. If this is correct, the code you shared will require little bit adjustment so that the code should be triggered only when user submit the form as guest user.
add_action('cred_save_data', 'func_set_custom_author_for_guest_user',10,2);
function func_set_custom_author_for_guest_user($post_id, $form_data){
if ($form_data['id']==99999 and !is_user_logged_in() ){
$my_post = array(
'ID' => $post_id,
'post_author' => 51
);
// Update the post into the database
wp_update_post( $my_post );
}
}
Where:
- you should adjust the 99999 with your original form ID.
add_action('cred_save_data', 'func_set_custom_author_for_guest_user',10,2);
function func_set_custom_author_for_guest_user($post_id, $form_data){
$forms = array( 99999, 88888, 77777 ); // replace with your original form IDs
if ( in_array( $form_data['id'], $forms ) and !is_user_logged_in() ) {
$my_post = array(
'ID' => $post_id,
'post_author' => 51
);
// Update the post into the database
wp_update_post( $my_post );
}
}
Where:
- Pleaser replace 99999, 88888, 77777 with your original form IDs.