wp_after_insert_post running twice when addding post from dashboard

36 views Asked by At

I can't prevent wp_after_insert_post hook from running multiple times. Explored many solutions from google but no help. Here is my code sample.

add_action( 'wp_after_insert_post', 'test', 99, 3);

function test($post_id, $post, $update){
    if(get_post_type($post_id) != 'service' || $post->post_status != 'publish') return;
    
    if($update){
        $data = [];
        $data[] = $post->post_type;
        $data[] = $post->post_status;
        wp_insert_post([
            'post_type' => 'post',
            'post_status' => 'publish',
            'post_title' => 'temp',
            'post_content' => implode('<br>', $data)
        ]);
    }
}

How to make sure that the hook will run only once.

0

There are 0 answers