Support

Account

Home Forums Front-end Issues How Update a post in front-end with acf (acf_form)

Solved

How Update a post in front-end with acf (acf_form)

  • Hey,
    My snippet works properly when I want to create the post, I just can’t update the created post. I can’t understand how to update the post with acf_form action in the front end, thanks for your help.

    std-update.php

    acf_form(array(
         'post_id'		=> '',
         'field_groups'	=> array( 19250 ),
         'post_title'	=> false,
         'updated_message' => __("Post updated", 'acf'),
         'submit_value'	=> 'Send',
         'return' => ''
      ));

    function.php

    	function register_update_std_profile( $post_id )
    	{
    	    if( $post_id == 'new_post' ) {
    	        // Create a new post
    	        $post = array(
    	            'post_title' => $_POST['fields']['field_123456'],
    	            'post_type'  => 'students_profile',
    							'post_status'	=> 'publish'
    	        );
    
    	        // insert the post
    	        $post_id = wp_insert_post( $post );
    
    	        return $post_id;
    	    }
    	    else {
    	        return $post_id;
    	    }
    	}
    	add_filter('acf/save_post' , 'register_update_std_profile');
  • In the acf_form() call you need to provide the post ID of the post that needs to be edited.

    
    if (some condition) {
      // the if needs to detect if a post is being edited
      // if it is then set $post_id to the post to be edited
      $post_id = $post=>ID; // something like this
    } else {
      // otherwise this is a new post
      $post_id = 'new_post';
    }
    
    acf_form(array(
         'post_id'		=> $post_id,
         'field_groups'	=> array( 19250 ),
         'post_title'	=> false,
         'updated_message' => __("Post updated", 'acf'),
         'submit_value'	=> 'Send',
         'return' => ''
      ));
    
  • Thank you, John
    It’s my crazy to forgot to use the acf_form() in a while loop to update a specific post, also you’re right I forgot to pass the post ID to the action too.
    Here is my complete code to update a specific post:

    while ( $author_posts->have_posts() ) : $author_posts->the_post();
    
        acf_form(array(
            'post_id'       => get_the_ID(),
            'field_groups'	=> array( 15 ),
            'post_title'    => false,
            'post_content'  => false,
            'submit_value'  => __('Update meta')
        ));
        endwhile;
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘How Update a post in front-end with acf (acf_form)’ is closed to new replies.