Suppose we need to publish a post only if some check is passed. To do this, we add the post to the database with a status of pending. Then we do our test and if it passed we publish the post, but if it didn't pass we do nothing.
function add_coin_post(){
//Add the post to the database
$post_data = [
'post_type' => 'coin',
'post_title' => $args['name'],
'post_name' => sanitize_title( $symbol ),
'post_status' => 'pending', // ! IMPORTANT
];
$post_id = wp_insert_post( wp_slash( $post_data ), true, false );
if( is_wp_error( $post_id ) ){
return $post_id;
}
// DO THE CHECK WE NEED
// where the variable $check_is_ok will be defined, if everything is ok
if( $check_is_ok ){
// run the publication hooks on which the
// operations for a new published post
wp_publish_post( $post_id );
}
}