Support

Account

Home Forums ACF PRO How to edit post title with acf_form

Solved

How to edit post title with acf_form

  • I have an acf_form set up where users can add a new post, and the post’s title would be generated by a custom field. It’s working for adding the new post.

    However, when I try to edit that post afterwards using a similar call of acf_form, I can’t get the custom field to change the post title.

    I’m using this for my acf_form() call:

        $args = array(
        	'post_id' => $post->ID,
        	'field_groups' => array( 4 ),
        	'submit_value' => 'Submit Changes',
        	'return' => '%post_url%'
        );
        
        acf_form( $args ); 
    

    And here’s what’s in my functions.php file:

    function my_pre_save_post( $post_id )
    {
        // check if this is to be a new post
        if( $post_id != 'new' )
        {
            return $post_id;
        }
    
        // Create a new post
        $post = array(
            'post_status'  => 'publish',
            'post_title'  => $_POST['acf']['field_1234'],
            'post_type'  => 'post'
        );  
    
        // insert the post
        $post_id = wp_insert_post( $post ); 
    
        // update $_POST['return']
        $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );    
    
        // return the new ID
        return $post_id;
    }
    
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    

    Basically, just trying to get that custom field to change the post’s title, but I think I need a push in the right direction.

    EDIT: Was able to fix this by adding to my functions file:

        // check if this is to be a new post
        if( $post_id != 'new' )
        {
            $postData = array(
                'post_title' => $_POST['acf']['field_1234'],
            );
            wp_update_post($postData);
            return $post_id;
        }
    
  • ckirk, how did you get the title to set from another field?

    I need that to work, but I keep getting “no-title” when the post is created.

    Where did you get “field_1234”? Is it the field key?

  • Hi @JustinCase

    You can find the field’s keys by editing a field group, clicking on ‘Screen Options’ and toggling the option to show field keys

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

The topic ‘How to edit post title with acf_form’ is closed to new replies.