From the documentation for post_exists()
:
Return
(int) Post ID if post exists, 0 otherwise.
So you can get the ID just by assigning the result of post_exists()
to a variable:
$post_id = post_exists( $class_post_title );
The same thing works for wp_insert_post()
:
Return
(int|WP_Error) The post ID on success. The value 0 or WP_Error on failure.
So you can do this:
$post_id = post_exists( $class_post_title );
if ( ! $post_id ) {
$post_id = wp_insert_post( $class_post );
}
// $post_id is now the ID of whichever post exists or was created.