Support

Account

Home Forums Front-end Issues Getting the post_id after creating a post with acf_form()

Solved

Getting the post_id after creating a post with acf_form()

  • Hello!
    I think this may be simple to do but I can’t get it to work

    I’ve created an acf_form() than creates a custom post on submit and redirects to a thanks you page, my question is, is there any way to get the new post_id there so I can display the info?

    I see there is a acf/pre_save_post filter but I would need something where I can get the post_id and maybe store it to session meanwhile or something

    Thanks!

  • Hi @polsola

    You should be able to do it by using the wp_redirect() function with the acf/save_post hook. But, you need to pass the ID to redirect URL like this:

    function my_acf_redirect_after_save( $post_id ) {
        
        // Only do it for "custom_post" post type
        if( get_post_type($post_id) != 'custom_post' ){
    		return;
    	}
    	
    	// Only do it on the front end
    	if( is_admin() ){
    		return;
    	}
        
        wp_redirect( 'https://example.com/thank/you/page/?postid=' . $post_id );
    
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_redirect_after_save', 99);

    Then you can get the ID on the thank you page like this:

    $post_id = $GET['postid'];

    I hope this helps ๐Ÿ™‚

  • Hello James,

    Thanks a lot it works! Just one thing, I have to add a die() after the wp_redirect() because if not it keep going to the ?updated=true

    Thanks!

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

You must be logged in to reply to this topic.