wp_insert_post()
returns either one of three things
-
the newly created post’s ID
-
a
WP_Error
object if$wp_error
is set to true if an error occured during post insertion in which case the post is not inserted -
0
if$wp_error
is set to false (default) if an error occured during post insertion in which case the post is not inserted
The following should work
$args = [
// All your post arguments
];
$q = wp_insert_post( $q );
if ( 0 != $q
&& !is_wp_error( $q )
) {
echo 'My new post ID is ' . $q;
}