The wp_update_post
hook will call the same action twice, as the action save_post_{$post->post_type}
is called in this function. I would add remove_action( 'save_post_tribe_events', 'set_to_pending');
before wp_update_post( $post );
and add_action( 'save_post_tribe_events', 'set_to_pending', 10, 3 );
after it works 🙂
function set_to_pending($id, $post, $update){
$the_post = print_r($post, true);
$the_post_author = $post->post_author;
$the_post_url = get_edit_post_link($id);
$the_post_url = wp_specialchars_decode($the_post_url);
if($the_post_author ==1 || $the_post_author == 2) {
} else {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
if (wp_is_post_revision($id)) {
return;
}
if (wp_is_post_autosave($id)) {
return;
}
// if new post
if (!$update) {
return;
}
if($post->post_status == 'trash') {
return;
}
$post->post_status="pending";
remove_action( 'save_post_tribe_events', 'set_to_pending');
wp_update_post( $post );
$times = did_action('save_post_tribe_events');
if( $times === 1){
wp_mail('[email protected]', 'Pending Event', $the_post_url);
}
//Add action here to prevent sending mail twice
add_action( 'save_post_tribe_events', 'set_to_pending', 10, 3 );
}
}
add_action( 'save_post_tribe_events', 'set_to_pending', 10, 3 );