Support

Account

Home Forums General Issues Unable to set title in ACF form created CPT post

Solving

Unable to set title in ACF form created CPT post

  • I’ve created a form on a site which gathers info in fields then creates a custom post with all the fields.

    I’ve got everything working bar 1 thing – setting the post title. The posts created have ‘no title’ and I’m stumped as to why. All the data submitted is present, but I just can’t figure out why one of the fields won’t set the post title:

    Here’s the filter in functions.php :

    
    function pre_save_post( $post_id ) {
        
        // stop function if not a new post
        if( $post_id !== 'new_post' ) {
            return $post_id;
        }
        
        // vars
        $name = $_POST['fields']['field_57c072a1a59c4'];
        $age = $_POST['fields']['field_57c06e718908c'];
        $date_of_birth = $_POST['fields']['field_5b0fedaf52688'];
      
        // Create a new post
        $post = array(
            'post_status'   => 'publish',
            'post_type'     => 'male_companions',
            'post_title'    => $name,
            'post_author' => $current_user->ID,
        );  
    
        // insert the post
        $post_id = wp_insert_post( $post ); 
        
        // return the new ID
        return $post_id;
        
    }
    

    All the POST fields work and save data fine, so I know they are correct, but I’m trying to use the $name for the post title.

    Here’s the acf_form code:

    
     <?php
                    if(isset($_GET['updated'])){
                        if($_GET['updated'] == 'true'){
                            echo '<p>The profile data has been saved.</p>';
                        } 
                    } 
                    ?>
                    <?php 
                      acf_form(array(
                          'id' => 'my-custom-post',
                          'post_id'       => 'new_post',
                          'field_groups'  => array(65),
                          'submit_value'  => 'Submit',
                          'new_post'      => array(
                            'post_type'   => 'my_custom_post',
                            'post_status' => 'publish'
                          ),
                    ));
       ?>
    

    Like I say, everything saves fine but I can’s set the post title – am I missing something?

    Secondly, is there a way to set the permalink from the new post too – like a sanitized field from the form – maybe the $name but with hyphens etc?

    Thanks in advance

  • Hi @phil-owen

    If you are using the latest version of the plugin, you need to change your syntax to the following:
    $name = $_POST['acf']['field_57c072a1a59c4'];
    You may also want to hook into the acf/save_post filter instead of the acf/pre_save_post.

    You can also change the slug by making use of the ‘post_name’ argument when generating the $post data.

    I hope this info helps.

  • HI @james

    Thank you for you reply.

    I’ve changed the syntax and also updated to acf/save_post. All still being saved correctly and the new posts created, but I still can’t get the title set.

    Here’s how I’m trying to fetch it:

    
    $cpt_title = get_field('name', $post_id);
      
        // Create a new post
        $post = array(
            'post_status'   => 'publish',
            'post_type'     => 'my_custom_post',
            'post_title'    => $cpt_title,
            'post_name'   => wp_unique_post_slug( sanitize_title( $cpt_title ) ),
            'post_author' => $current_user->ID,
        );  
    

    But it doesn’t set the title and understandably the slug.

  • Any ideas on this? Still can’t get the title added from the ACF form.

  • Hey @phil-owen ,

    Just to be sure, why don’t you use the default acf title on acf_form function? Something like that:

    acf_form(array(
    "post_title" => true,
    [....]
    ))

    P.s.: Sorry about my english

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.