After creating Custom post type by user delete old one

I think it would be helpful if you could explain why you’re generating courses out of edits that a user’s doing on his profile. The simplest solution seems to be a frontend form that just allows a user to create new posts or edit existing ones rather than going for this alternative solution where you’re using the data from the profile page to create new posts.


If you want to stick to the current implementation, I believe it would be useful for you to look into status transitions: https://developer.wordpress.org/reference/hooks/transition_post_status/

Here’s some example code that I used to create a post in a the category with the ID 1 (= news) after a custom post type had been created:

// Automatically create a news when a wiki post has been published
function nxt_create_news($new_status, $old_status, $nxt_wiki_post) {
    if('publish' === $new_status && 'publish' !== $old_status && $nxt_wiki_post->post_type === 'wiki') {
        $nxt_post_author = $nxt_wiki_post->post_author;
        $nxt_wiki_permalink = get_post_permalink($nxt_wiki_id);
        $nxt_wiki_title = $nxt_wiki_post->post_title;
        $nxt_postarr = array(
            'post_author' => $nxt_post_author,
            'post_content' => 'The wiki entry ' . $nxt_wiki_title . ' has just been published.<br /><br /><a href="' . $nxt_wiki_permalink . '" title="' . $nxt_wiki_title . '">Check out wiki entry</a>.',
            'post_title' => 'The wiki entry ' . $nxt_wiki_title . ' has been published',
            'post_status' => 'publish',
            'post_category' => array(1),
        );
        $nxt_post_id = wp_insert_post($nxt_postarr);
        set_post_thumbnail(get_post($nxt_post_id), '1518');
    }
}
add_action( 'transition_post_status', 'nxt_create_news', 10, 3 );

In your specific case, you can compare parts of the saved posts to the new post that would be created and then prevent the default action from happening (a new post being created) and instead just edit the old post.

The main question would be how would the system know which post someone’s trying to edit? I can’t really answer that as I couldn’t make out why you’re using the profile edits to create new posts I’m afraid, but I hope this is helpful regardless. 🙂