Home Forums Front-end Issues Using acf_form for multiple post_type


Using acf_form for multiple post_type

  • Hi Eliot —

    I know this is an old post but I’d like to know exactly how to utilize the html_after_fields to create multiple front-end post-creation forms.

    I am not a PHP expert but I have successfully created a front-end form for one custom post type — I just don’t know how to modify it for additional forms (for other custom post types).

    Thanks for the instruction.


  • Hi @enricospada

    I don’t understand your question:
    how to utilize the html_after_fields to create multiple front-end post-creation forms.

    Can you please be specific about your question? The above was a bit vague.


  • Thanks for the response. Sorry for the confusion.

    My post referred to this post:

    And your answer:

    multiple pre_save_post
    The trick here is to use the ‘html_after_fields’ arg to add in a hidden input with a custom value. This will get posted to the save action which you can then use to determine which form has been posted.

    I’d like to know how to have multiple front-end forms that each create posts of different custom post types. Your response naveen009 suggested that the solution lies in the html_after_fields; but whatever the solution, I’d like to learn!

    Thanks again for a great plugin.


  • Hi Enrico,

    I think I see what you mean 🙂 So you’re creating a repeated field which has these fields (just an example):

    1. Title
    2. Post Type
    3. Post Content

    When user’s save the field you would like to create posts based on the information the user has entered.

    If this is correct you could use the acf/save_post action. This runs once the values for the repeater are saved. You can then retrieve them using the usual method, you could do something like this:

    add_action( 'acf/save_post', 'my_custom_creator' );
    function my_custom_creator( $post_id ) {
        if( have_rows('your_repeater') ):
            while ( have_rows('your_repeater') ) : the_row();
            $postdata = array(
                'post_title' => get_sub_field( 'title' ),
                'post_content' => get_sub_field( 'content' ),
                'post_type' => get_sub_field( 'post_type' );
            $new_post_id = wp_insert_post( $postdata );

    Do be careful though as this will create a new post each time you save the fields. You can either delete the post where the options are right after creating the new post, or you can save the ID’s of the newly created post and make sure they are not created, only updated if the options are saved again.

    Hope I could help,


  • Thanks, Daniel. That’s really useful info which I’m sure I’ll use in the future.

    However, right now I’m trying to do this through separate forms. That is, I have multiple forms on my site but they each update a different post-type.

    This is in my functions.php:

    function my_pre_save_post( $post_id )
        // Create a new post
        $post = array(
            'post_status'  => 'publish' ,
            'post_title'  => $_POST['fields']['field_52d978ea24ac7'],
            'post_type'  => 'location' ,
        // 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' );

    This makes sense and works perfectly for one form — but if I add a second form to update a second post type, it doesn’t work. What do I need to modify so that I can have more than one form?


  • Hi @enricospada

    Thanks for the clarification. For your filter to know which form posted the data, you will need to use the ‘post_id’ parameter.

    If you look at the tutorial here:

    You will see that the filter function contains a check on the $post_id parameter:

    // check if this is to be a new post
        if( $post_id != 'new' )
            return $post_id;

    This ‘new’ comes from the acf_form $options array and you can customize it!

    Therefore, on form 1, you could set this as ‘new_1’, and on form 2 ‘new_2’, etc.

    Then you can set up 2 functions hooked into the pre_save_post filter and use the validation like so:

    // check if this is to be a new post
        if( $post_id != 'new_1' )
            return $post_id;

    Hope that helps.


  • Gosh, thanks! That’s so clear. I think I removed the $post_id check based on another tutorial blog post, so the answer to my current issue wasn’t appearing to me. Now that you’ve told me, I realize it’s quite clear. Thanks so much for the help.

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

The topic ‘Using acf_form for multiple post_type’ is closed to new replies.