Hi,
I have added some custom fields to the WordPress registration form. When a user registered I wan’t to generate a post how can I get the values from the registration form? For example I would like to retrieve the company field and assign it to the post title.
function create_new_user_posts($user_id){
if (!$user_id>0)
return;
$my_distributor_post = array(
'post_type' => 'distributer_search',
'post_title' => 'custom field company',
'post_content' => '',
'post_status' => 'draft',
'post_author' => $user_id,
);
// Insert the post into the database
$contact = wp_insert_post( $my_contact_post );
update_user_meta($user_id,'_distributor_post', $distributor);
}
add_action('user_register','create_new_user_posts');
If your create_new_user_posts() function above works, try adding the below to your code; substitute your field keys and post type.
function pre_save_company_name($post_id){
if( empty($_POST['acf']) ) { return; }
$company = $_POST['acf']['THE COMPANY NAME FIELD KEY'];
return $company;
}
add_action('acf/save_post', 'pre_save_company_name', 21);
function company_name_as_post_title( $data, $postarr ) {
if ( ! $postarr['ID'] ) return $data;
if ( $postarr['post_type'] !== 'YOUR POST TYPE' ) return $data;
$post_id = $postarr['ID'];
$company = pre_save_company_name($post_id);
if( empty($company) ) { return $data; }
$data['post_title'] = $company;
return $data;
}
add_filter( 'wp_insert_post_data', 'company_name_as_post_title', 99, 2 );
Thanks for your response I managed to to solve this by doing the following
$user_company = get_field('reg_company_name', 'user_' . $user_id);
$my_distributor_post = array(
'post_type' => 'distributer_search',
'post_title' => '$user_company',
'post_content' => '',
'post_status' => 'draft',
'post_author' => $user_id,
);
This works because the post is generated after the user is created.