Support

Account

Home Forums Front-end Issues Pass new $post_id to url or…

Solved

Pass new $post_id to url or…

  • Hi @elliot,

    Im creating a front end post form. I’d like the next page after the user submits a form to be a publish page.

    All I need is to somehow retrieve the newly created post’s ID from the form, once the post has been inserted into the DB.

    I see the the function for the pre_save_post is returning the newly created $post_ID, but how do I retrieve that and possibly add it as a query to my ‘return’ url for the next page?

    So basically, if the new post ID is 123, I’d like to create the return url to be domain.com/?pid=123

    Thanks!

  • Hi @synergywp

    The acf_form function will not create a new post by default. To create a new post, you will need to hook into the pre_save_post filter and create the post there.

    Whilst you are inserting the post, you can get the new ID from the code you write.

    Then you can modify the $_POST data which contains the redirect url.

    Take a look at the docs for examples of creating a new post with the acf form and also read through the source code core/api.php to se how the acf_form_head function works

    Cheers
    E

  • Hi @elliot,

    Yes, I’ve followed the docs to create the front end form and it works fine. I see that the function creates a new post when the post_id == ‘new’, and the WP core does that automatically if I remember, if a post ID isn’t supplied when creating a post.

    However, I’m unsure how to retrieve the post ID once ACF has created it. I know how to insert it after for the redirect URL, just not sure how to throw it into a variable to begin with.

    Thanks.

  • Hi @synergywp

    No worries. Taking the code from the doc example, I have added the new post_id to the return data which ACF will then redirect to!

    
    <?php 
     
    function my_pre_save_post( $post_id )
    {
        // check if this is to be a new post
        if( $post_id != 'new' )
        {
            return $post_id
        }
     
        // Create a new post
        $post = array(
            'post_status'  => 'draft' ,
            'post_title'  => 'A title, maybe a $_POST variable' ,
            'post_type'  => 'post' ,
        );  
     
        // 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' );
     
    ?>
    
  • @elliot,

    Thank you so very much! Works perfectly.

  • Update:

    The above code will not work in ACF v5 due to the redirect url not being stored at $_POST['return'].

    Instead, please use $GLOBALS['acf_form']['return'] to modify the return url.

    Thanks
    E

  • Hi @elliot

    EDIT: Okay, I was able to fix it not going to /publish/ but now it’s not adding the query args still. Thanks.

    Thanks for responding to this after almost 2 years. I was in the middle of creating a new service when ACF 5 came out, so I didn’t have issues with implementing it. However, I’m now updating my other site for ACF5 and I’ve ran into this issue with the redirect.

    I’ve tried a few things and I can’t seem to get it to redirect to a) the declared page (in my case, a /publish/ page… I am saving submission as draft and the publish page is for a payment gateway.) and b) with the query args needed to supply the logic to the publish page.

    Any thoughts on this? I’m happy to share my source code. Your plugin has made two pretty large sites successful.

    $args = array(
    							'post_id' => 'new_post',
    							'new_post' => array(
    						        'post_status' => 'draft',
    						        'post_type'  => $cpt,
    						        //'post_title' => 'new listing'
    							),
    							'html_before_fields' => $htmlbefore,
    							'field_groups' => $fieldgroups,
    							'form_attributes' => array('class' => $formattr,'autocomplete' => 'false'),
    							'return' => home_url('publish/'),
    							'submit_value' => 'Submit Listing'
    						);
    
    						acf_form( $args );

    That’s the form.

    And here is one of the pre_save_post functions:

    function my_pre_save_post( $post_id )
    	{
    	    // check if this is to be a new post
    	    if( $post_id != 'new' ) {
    	        return $post_id;
    	    }
    
    	    // Get Post Type
    	    $listing_type = $_POST['acf_form']['bh_post_type'];
    	    $perftype = $_POST['acf_form']['bh_perf_org_type'];
    	 
    	   // Create a new post
    	    $post = array(
    	        'post_status' => 'draft',
    	        'post_type'  => $listing_type,
    	        'post_title' => 'new listing'
    	    );  
    	 
    	   // insert the post
    	    $post_id = wp_insert_post( $post );
    
    	    if ($perftype) {
    	    	update_post_meta( $post_id, 'bh_perf_org_type', $perftype );
    	    }
    
            $query_args = array(
                'pid' => $post_id,
                'step' => 'pick',
                'ptype' => $listing_type,
                'msg' => 'success'
            );
    
            
    
    	    // update $_POST['return']
        	 $_POST['acf_form']['return'] = add_query_arg( $query_args,  $_POST['acf_form']['return'] );
    	 
    	    // return the new ID
    	    return $post_id;
    	}
     
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );

    I know you said $GLOBALS above there and I tried that, but no go, so I went back to $_POST. I didn’t run into this with my other site but it’s also set to return to the same page with just some queries (post is added through a modal).

    Thanks in advance if you notice anything!

    Chris

  • Hi @synergywp

    I am confused.
    What version of ACF are you using? v4 or v5(PRO)?

    If you are using ACF PRO, you have used the wrong acf_form documentation. Please note the documentation versions: http://www.advancedcustomfields.com/resources/acf_form/

    In short, there are a few issues with your code so PLEASE do lots of debugging line by line to make sure your code is actually running.

    1. The args …

    
    'post_id' => 'new_post',
    							'new_post' => array(
    						        'post_status' => 'draft',
    						        'post_type'  => $cpt,
    						        //'post_title' => 'new listing'
    							),
    

    … are only available in ACF5 (ACF PRO).

    2.

    Your code:

    
    if( $post_id != 'new' ) {
    	        return $post_id;
    	    }
    

    … will most likely always return, because your post_id is ‘new_post’, not ‘new’.

    PLEASE debug your code line by line to ensure it is running and all variables are correct. Also debug the $GLOBALS vs $_POST array to see what data is available.

    Thanks
    E

  • Yea, I was updating from v4 to v5.

    The second post of code, as you obviously know, was used to actually use the form data to create a post.

    $_POST['bh_post_type'];

    Was used for v4 obviously. I tried appending $GLOBALS[‘acf_form’] to that in place of $_POST and it did not work. So I tried reverting it back to $_POST to see if an object being sent over instead of single variables.

    I noticed the new vs new_post shortly after posting and still didn’t work.

    I’ll keep debugging and report back when I’m able. Thanks.

  • Hi @elliot

    Okay, I’ve worked on this a bit.

    I think the confusion before was the fact I was trying to migrate from V4 to V5. It seems I’ve discovered the following, and please correct me if I am wrong:

    – I see the V5 no longer needs acf/pre_save_post filter to insert a post. However, I was relying on this to add_query_arg a few variables. Specifically, the newly created post ID and the listing type which was being pulled with:
    $listing_type = $_POST['bh_post_type']

    If it’s a successful post, I want to simply add 4 query arguments, 2 of those being what I stated above. In V4, i used that filter to handle the post insert and return URL just as you had laid out for me earlier in this thread.

  • Hi @synergywp

    You can continue to use the acf_form exactly as you have been doing in v4.
    ACF v4 does not contain settings for ‘new_post’.

    It looks like you have merged both ACF v4 and ACF v5 functionality.

    If you want to use the acf/pre_save_post filter, and create your own post, please do so, but please remove the ‘new_post’ settings.

    Please follow the documentation here:
    http://www.advancedcustomfields.com/resources/acf_form/?version=4

  • Thanks @elliot

    I had done another extensive project with ACF5 and I think was making a few assumptions on handling of post update/insert.

    Aside from reverting back to ‘post_id’ => ‘new’ on the acf_form call and the pre_save_post, I changed the $_POST[‘return’] to $GLOBALS[‘acf_form’][‘return’] and it looks like we’re back to normal.

    As always, thanks for your time and an excellent plugin.

  • Hi @synergywp

    Nice one!
    Thanks for being a good sport about changes.

    Cheers
    E

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

The topic ‘Pass new $post_id to url or…’ is closed to new replies.