"transition_post_status" action creating two post with wp_insert_post

What I'm tiring, I've created a custom post-type called the recipe when creating a new post on the recipe, I have inserted a new post on another post-type it working fine but post created two times on every published post

    function post_create_on_publish_only( $new_status, $old_status, $post ) {

   if ( ( 'publish' === $new_status )
        'recipe' === $post-post_type
   ) {
      $my_post = array(
         'post_title'    = get_the_title($post),
         'post_status'   = 'publish',
         'post_type' = 'wpcf7_contact_form',
      );
      $get_post = get_page_by_title(get_the_title($post));
      if ( !is_page($get_post-ID)  did_action( 'transition_post_status' ) === 1){
         $id = wp_insert_post( $my_post );
      }
   }
}
add_action( 'transition_post_status', 'post_create_on_publish_only', 10, 3 );

How can I solve this issue? anyone can help me?

Topic wp-insert-post Wordpress

Category Web


I remember having a similar issue in a passed and checking if someone published custom post type first was solving the issue for me, something like this:

add_action( 'transition_post_status', 'post_create_on_publish_only', 10, 3 );
function post_create_on_publish_only( $new_status, $old_status, $post ) {

    if ( ( $new_status == 'publish' ) && ( $old_status != 'publish' ) && ( $post->post_type == 'recipe' ) ) {
        $my_post = array(
            'post_title'    => get_the_title($post),
            'post_status'   => 'publish',
            'post_type' => 'wpcf7_contact_form',
        );
        $get_post = get_page_by_title(get_the_title($post));
        if ( !is_page($get_post->ID) && did_action( 'transition_post_status' ) === 1){
            $id = wp_insert_post( $my_post );
        }
    } else {
        return;
    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.