Setting META tags such as description and title on programmatic post creation

When you use wp_insert_post to create the post, it will return the post id of the post you just created. The following code demonstrate how to add meta values:

$my_post_data = array(
            'post_title' => $collection,
            'post_type' => 'post',
            'post_content' => '<p style="margin-bottom: 10px;">' . $content . '</p>',
            'post_category' => array(
                $term->term_id
            ),
            'post_author' => 1,
            'post_status' => 'publish',
            'post_date' => date('Y-m-d H:i:s')
        );
$post_id = wp_insert_post( $my_post_data );

if ( ! $post_id && ! is_wp_error( $post_id ) ) {
    add_post_meta( $post_id, 'title', 'This is a title' );
    add_post_meta( $post_id, 'description', 'This is a description' );
}