Tell us what you are trying to do?
I have a custom post type ('Clients'), and I also have a user role setup for Clients.
In my 'Client' post type, I have a custom field of 'First Name'.
I have a user form setup which allows me to set up a new user (with the user role of Client), and at the same time as doing this, I also want my form to create a post (under my custom post type 'Client'), using the same details which are captured by the user form.
I want for the post title of my 'Client' post to be the email address captured by my user form. And I also want my 'First Name' custom field to be populated with the First Name captured by the user form when setting up the user.
I have been successful is some ways, but unsuccessful in others - using the code snippet here (https://toolset.com/forums/topic/auto-create-custom-post-after-new-user-submission/#post-493092) I am able to setup the 'Client' post which has the Post Title of the email address captured by the user form.
However, I am unable to get the 'First Name' custom field (in my Client post) to populate with the First Name from my user form. I am aware that the code snippet above does not contain anything to do with custom fields, so I also found this thread (https://toolset.com/forums/topic/using-woocommerce-and-cred_save_data-to-create-a-custom-post-type/) which seemed to suggest how to auto-populate a custom field, however I cannot get it to work (like I say, after I submit my user form, the Client 'User' is created, and also the Client 'Post' is created - also the Post Title of the Client Post is being populated with the email address, however my custom field of 'First Name' is NOT being populated with anything, when it should be getting populated with the 'First Name' from the User Form.
I hope that all makes sense! 🙂
Here is the code which I am using in my functions.php file:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
if ($form_data['id']==146)
{
// Create post object
$my_post = array(
'post_title' => $_POST['user_email'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => $post_id,
'post_type' => 'client'
);
// Insert the post into the database
wp_insert_post( $my_post );
// Update any custom fields on Client Post
$clientFirstName = get_user_meta( $user_id, $_POST['first_name'], true );
update_post_meta( $post_id, 'client-first-name', $clientFirstName );
}
}
** 146 is the ID of my User Form
** client-first-name is the slug of my custom field in my 'Client' custom post type
Can you tell me what it is that I am doing wrong?
Thank you very much for your help,
Keith
Is there any documentation that you are following?
https://toolset.com/forums/topic/auto-create-custom-post-after-new-user-submission/#post-493092
https://toolset.com/forums/topic/using-woocommerce-and-cred_save_data-to-create-a-custom-post-type/
Is there a similar example that we can see?
No
What is the link to your site?
Under development but can provide access if needed (please allow me to enter these in a private box), thanks 🙂