Gravity Forms Advanced Post Creation Add-On: get Id of last created post [RESOLVED]

Hello

I have a form which is creating a post, when form is submitted.
One same form submission, I want to create another post. To do this, i have used ‘gform_after_submission’ as follows:

  add_action("gform_after_submission", "set_post_content", 10, 2);
 function set_post_content($entry, $form){

  $my_post = array(
	'post_title'    => wp_strip_all_tags( $entry[1] ),
	'post_type'=>'product', 
	'post_content'  => $entry[2],
	'post_status'   => 'publish',
	'post_author'   => 1,
	//'post_category' => array( 8,39 )
);

$last_id = wp_insert_post( $my_post, true, true );
update_post_meta( $last_id, '_regular_price','333' );
echo '<pre>';
echo 'Form: <br/>';
print_r($form);
echo '<br/> Entry: <br/>';
print_r($entry);
echo '<br/> Last ID: <br/>';
print_r($last_id);
//die('ddd');
}

This way both posts are being created. Now i also need the ID of the post that is being created by Advanced Post Creation Addon as i need to build a relation between both posts.
From this post Multiple posts generated by one form « Gravity Support Forums i came to know that i can get ID of the post by using $entry["post_id"] but it is empty. Here is what i have in $entry object

 Array
(
 [id] => 22
 [status] => active
[form_id] => 1
[ip] => ::1
[source_url] => http://localhost/wp/?gf_page=preview&id=1
[currency] => USD
[post_id] => 
[date_created] => 2021-01-18 16:44:37
[date_updated] => 2021-01-18 16:44:37
[is_starred] => 0
[is_read] => 0
[user_agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36
[payment_status] => 
[payment_date] => 
[payment_amount] => 
[payment_method] => 
[transaction_id] => 
[is_fulfilled] => 
[created_by] => 1
[transaction_type] => 
[1] => Test post 2222
[2] =>  Test post 2222 Content  Test post 2222 Content  Test post 2222 Content
[3] => 59
[4] => 58
)

Please share, how can i get ID of last inserted post ID. I can also use any other action/hook that is getting fired after creating post using Advanced Post Creation plugin.

Thanks

Check out the action hook gform_advancedpostcreation_post_after_creation

The following values are passed to it – $post_id, $feed, $entry, $form

1 Like

Hello @uamv
That code worked. Thanks for your help.

2 Likes