Home Forums ACF PRO Frontend form submitting to custom post type Reply To: Frontend form submitting to custom post type

  • Hi!
    By following this thread I was able to do the same, thank you very much!
    Now I’m facing the problem of getting the user login from the from submission, in order to use it as the author of my CPT.
    Using codes below I’m able to retrieve the user login ID in order to create a post title as “user_ID – Title”, but the post author still remains empty.
    Can you help me please?
    Thank you very much!

    Here my codes.

    Form template:

    $new_post = array(
    		'post_id'            => 'new_post',
    		'field_groups'       => array(46),
    		'form'               => true,
    		'html_before_fields' => '',
    		'html_after_fields'  => '',
    		'submit_value'       => 'Submit Post',
    		'updated_message'    => 'Saved!'
    	acf_form( $new_post );


    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    function my_pre_save_post( $post_id ) {
    	// bail early if not a new post
    	if( $post_id !== 'new_post' ) {
    		return $post_id;
    	// vars
    	$title = $_POST['fields']['field_59c6d5f47898c'];
    	global $current_user;
    	// Create a new post
    	$post = array(
    		'post_status'	=> 'publish',
    		'post_type'		=> 'ws_trip',
    		'post_title'	=> $current_user->user_login.'-'.$title,
            'post_author'	=> $current_user->user_login,
    	// insert the post
    	$post_id = wp_insert_post( $post ); 
    	// return the new ID
    	return $post_id;