Skip Navigation

[Resolved] Creating a CPT immediately after creating a user

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to automatically create a post immediately after a new User form is submitted.

Solution: The best way to do this is to create a post with custom code using the Forms API. Here's an example: https://toolset.com/forums/topic/auto-create-custom-post-after-new-user-submission/#post-492026

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 10 replies, has 2 voices.

Last updated by Mario 4 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1260937

Tell us what you are trying to do?
As a user is created it simultaneously creates a CPT record.

What is the best way to do this and is is there an example.

#1261305

The best way to do this is to create a post with custom code using the Forms API. Here's an example: https://toolset.com/forums/topic/auto-create-custom-post-after-new-user-submission/#post-492026

#1261459

Hi Christian
I have used this code, it saves the new cpt but the title is blank. I want to save the user first and last name in the title. Just tested it with the first name but not working.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
if ($form_data['id']==23681)
{
$my_post = array(
'post_title' => $_POST['user_firstname'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => $user_ID,
'post_type' => 'cp-provider'
);

wp_insert_post( $my_post );
}
}

#1261511

Try first_name and last_name:

'post_title' => $_POST['first_name'] . ' ' . $_POST['last_name'],
#1261543

great.

Also
I have a field in the CPT i want to populate

'cp-mem-id' => $post_id, but it is not populating the cpt field

$my_post = array(
'post_title' => $_POST['first_name'] . ' ' . $_POST['last_name'],
'cp-mem-id' => $post_id,
'post_content' => 'xx',
'post_status' => 'publish',
'post_author' => $user_ID,
'post_type' => 'cp-provider'
);

#1261595

If you want to set custom field values, you need a meta_input array, and if cp-mem-id is a Types field, you must use the wpcf- prefix.

$my_post = array(
'post_title' => $_POST['first_name'] . ' ' . $_POST['last_name'],
'meta_input' => array(
        'wpcf-cp-mem-id' => $post_id
    ),
'post_content' => 'xx',
'post_status' => 'publish',
'post_author' => $user_ID,
'post_type' => 'cp-provider'
);
#1261627

Trying to reference the user first name and pass it to cpt

'meta_input' => array(
'wpcf-cp-mem-id' => $post_id,
'wpcf-cp-First-Name' => $_POST['last_name']
),

#1261647

First name or last name? Your code says last_name.

#1261649

First name

#1261651

Then you should change it to first_name:

'meta_input' => array(
'wpcf-cp-mem-id' => $post_id,
'wpcf-cp-First-Name' => $_POST['first_name']
),
#1261657

Just need to have the field all in lowercase and then it works.

Thank you for all your help.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.