You can hook your function to the wp_insert_post
action hook:
add_action( 'wp_insert_post', 'wpse128767_add_meta' );
function wpse128767_add_meta( $post_id ) {
add_post_meta( $post_id, 'key', 'value' );
}
To make sure your metadata has been added before any other insert hooks run, give it a higher priority:
add_action( 'wp_insert_post', 'wpse128767_add_meta', 1 );
By default add_action()
uses a priority of 10
; the lower the number, the earlier it runs.