Support

Account

Home Forums Front-end Issues Edit Post Form sets comment_status OFF

Solving

Edit Post Form sets comment_status OFF

  • HI All, Ive made a front end form that allows users to save a new post. It saves with the comment_status = ture, meaning users can leave comments on the new post.

    However if the post is edited using the front end form the comment_status turns off.

    NEW POST: (WORKS FINE, comment_status default is ON)

    	$new_post = array(
    		'post_id'            => 'new', // Create a new post
    		'field_groups'       => $field_groups, // Create post field group ID(s)
    		'form'               => true,
    		'return'             => '%post_url%', // Redirect to new post url
    		'html_before_fields' => '',
    		'html_after_fields'  => '',
    		'submit_value'       => 'Submit Post',
     		'updated_message'    => 'Saved!'
    	);
    	acf_form( $new_post );

    EDIT/UPDATE POST: (Why does comment_status get turned OFF???)

    
            $edit_post = array(
    		'post_id'            => $pid, // Edit post
    		'field_groups'       => array(8832), // Create post field group ID(s)
    		'form'               => true,
    		'return'             => '%post_url%', // Redirect to new post url
    		'html_before_fields' => '',
    		'html_after_fields'  => '',
    		'submit_value'       => 'Update Post',
        	'updated_message'    => 'Saved!'
    	);
    	acf_form( $edit_post );

    Please help! 🙂 Thanks

  • Hi @adeon

    I believe you’re using ACF free version. That means that you need to use extra functions to save the post. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/?version=4.

    I’m not really sure why it changes the comment status, bu you can always use the acf/save_post hook to change it back like this:

    function my_acf_save_post( $post_id ) {
        
        // Only do it for the front end form
        if( is_admin() ){
            return;
        }
        
        // set the comment status
        $my_post = array(
            'ID' => $post_id,
            'comment_status' => 'open',
        );
        
        // remove action to avoid infinite loop issue
        remove_action('acf/save_post', 'my_acf_save_post', 20);
        
        // update the post
        wp_update_post( $my_post );
        
        // add the action back
        add_action('acf/save_post', 'my_acf_save_post', 20);
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    I hope this helps 🙂

  • HI James,

    Thanks heaps for the feedback.. Im Using ACF FORMS PRO. Ill give the above a try and let you know how I go.

    Regards

    Adeon

  • Hi @adeon

    In that case, you need to set the ‘post_id’ option to ‘new_post’ instead of ‘new’ like this:

    'post_id' => 'new_post',

    Thanks 🙂

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

The topic ‘Edit Post Form sets comment_status OFF’ is closed to new replies.