How to Populate a Field with a New Post ID

When creating a new post using the Post Management add-on, you might want to populate a field on your form with the new post ID. This can be achieved by adding a Run WordPress Hook action to your form that calls a simple PHP script. The PHP script returns the new post ID using the field_value return action.

Run WordPress Hook Action

The Run WordPress Hook action would be configured as follows:

Type: Filter
Hook Tag: set_post_id (This can be changed to any hook tag name)
Priority: After other actions

WS Form - Get Post ID Into Field - Run WordPress Hook

PHP Script

An example PHP script is shown below. In this example, you would change the field_id parameter to be ID of the field you want to populate with the post ID.

add_filter( 'set_post_id', 'set_post_id_hook_function', 10, 2 );

function set_post_id_hook_function( $form, $submit ) {

    // Get post ID
    $post_id = isset( $submit->post_id ) ? $submit->post_id : 0;

    return empty( $post_id ) ? $submit : array(

        // If post ID set, set the hidden field value
        array(

            'action' => 'field_value',
            'field_id' => 123,
            'value' => $post_id,
        )
    );
}