Support

Account

Home Forums Front-end Issues Set Post Status and Post Parent via custom field

Solving

Set Post Status and Post Parent via custom field

  • Is there a way to set the post parent or post status via a ACF custom field?

    If not, what might be a good way to populate these fields on submission using a front-end form? and prepopulate them when the form is loaded after they are saved?

  • Hi @maxaud

    The acf_form() functions has the new_post option to set the new post data. Please take a look at this page to learn more about it: https://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/.

    I hope this helps 🙂

  • I’d like to update a post, not set it upon creation.

  • Hi @maxaud

    In that case, you need to create dummy custom fields to create the form. You can also add the input fields manually using “html_after_fields” option, but it will be more complex.

    For the post status, you can create a select field type while for the parent post, you can use a post object field type. After that, you can update the post using the acf/save_post and wp_update_post() function. Maybe something like this:

    function my_acf_save_post( $post_id ) {
        
        // Get the selected post status
        $value = get_field('post_status_field', $post_id);
        
        // Update current post
        $my_post = array(
          'ID'           => $post_id,
          'post_status'   => $value,
        );
    
        // Remove the action to avoid infinite loop
        remove_action('acf/save_post', 'my_acf_save_post', 20);
        
        // Update the post into the database
        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);

    Hope this helps 🙂

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

The topic ‘Set Post Status and Post Parent via custom field’ is closed to new replies.