Celebrate WordCamp Europe - 30% OFF! Use coupon WC30 - View Details

How to Get the Post ID after Creating a Post

The WS Form Post Management add-on is a great tool for creating new posts in WordPress. Once a post has been created, you may want to obtain the post ID that was created so that you can use it elsewhere such as a redirect, WordPress hook or perhaps sending it to a third party integration.

The WS Form #post_id Variable

After a post is created, you can use #post_id in subsequent actions to obtain the new post ID.

For example, you could use it in the custom mapping of the Google Sheets action:

WS Form - #post_id variable - Custom Mapping

Custom mapping settings exist in most third party integrations.

You could also add it directly to the content of a Show Message action:

WS Form - #post_id variable - Content

The $submit Object post_id Property

When a new post is created, the post ID is also added to the $submit object as a post_id property.

When using the Run WordPress Hook action, you can access the newly created post ID using:

$submit->post_id

An example hook function using this might be:

// Filter function
function wsf_filter_function( $form, $submit ) {

    // Get post ID
    $post_id = $submit->post_id;

    // Add term to post                        
    wp_set_post_terms( $post_id, 'my-term', 'my-taxonomy', true );
}

// Add filter
add_filter( 'wsf_filter_tag', 'wsf_filter_function', 10, 2 );