0

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 );

enter image description here

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

1 Answer 1

0

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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.