Support

Account

Home Forums General Issues acf form: edit post title and post_status

Helping

acf form: edit post title and post_status

  • Hey,

    I’m using acf_form to create new and edit existing posts of CPT ‘discs’.

    This is my code in functions.php:

    // Create the frontend form
    function my_pre_save_post( $post_id ) {
    	if ( $post_id != 'new' ) {
    		return $post_id;
    	}
    
    	$post = array(
    		'post_status' => 'draft',
    		'post_title' => $_POST['fields']['field_5479cd3485fcf'],
    		'post_type' => 'discs'
    		);  
    
    	$post_id = wp_insert_post($post); 
    
    	$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );    
    	
    	return $post_id;
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );

    This is my code to add new ‘discs’ posts:

    $args = array(
                'post_id' => 'new',
                'field_groups' => array(177, 100, 104, 111, 113, 136),
                'field_el' => 'div',
                'return' => home_url() . '/discs/#new-success',
                'submit_value'      => 'Create',
                'updated_message' => 'Done.'
             );
    
    acf_form( $args );

    And this is my code to edit existing ‘discs’ posts:

     $args = array(
                  'post_id' => get_the_ID(),
                  'field_groups' => array(100, 104, 111, 113, 136),
                  'form' => true,
                  'return' => home_url() . '/discs/#edit-success',
                  'html_before_fields' => '',
                  'html_after_fields' => '',
                  'post_status'  => 'draft',
                  'submit_value' => 'Update',
                  'updated_message' => 'Update done.'
               );
    acf_form( $args );

    However, add new posts and edit existing posts works fine.
    Additionally I want two things:

    1) After editing a post, set the post_status to ‘draft’ or ‘pending’.
    2) When creating a new post, get the value from input field (name / field_5479cd3485fcf) and set this as the new post_title.

    I tried a lot but nothing worked for me.

    Thanks for your help!

  • I just replied to another post that is along the same lines as this one. The concept of that you want to do is there. http://support.advancedcustomfields.com/forums/topic/radio-field-conditional-in-afc_form/

    In the pre_save_post function to do 1 your would include code in the if

    
    if( $post_id !== 'new_post' ) {
        // change post status
        return $post_id;	
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘acf form: edit post title and post_status’ is closed to new replies.