Can anyone help me to create a custom post type content when a new user is registered? But the user should be of specific role only.
I have been searching but couldn't find any solution.
Try below code:
add_action( 'user_register', 'create_new_post_onuser_registration', 10, 1 );
function create_new_post_onuser_registration( $user_id ){
// Get user info
$user_meta = get_userdata( $user_id );
$user_roles = $user_meta->roles;
// Update the role here
if ( in_array( 'subscriber', $user_roles ) ) {
// Do something.
// Create a new post
$subscriber_post = array(
'post_title' => $user_meta->nickname;
'post_content' => $user_meta->description,
'post_type' => 'custom_post_type', // Update to your custom post type
);
$post_id = wp_insert_post( $subscriber_post );
// Add custom data
add_post_meta( $post_id, 'user_email', $user_meta->user_email );
}
}