Changing new post to “pending” on publish – but “Publish failed” – why?

Question

I only want 2 admis to be able to publish events. So I want to change any new event published by any other user to be set to "pending" when the post is "published." Which, I guess, technically, means that it won’t get published. But, anyway, this is what I’ve tried:

   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'; 
        wp_update_post( $post );

        $times = did_action('save_post_tribe_events');
        if( $times === 1){
            wp_mail('someone@mail.com', 'Pending Event', $the_post_url);
        }
        
    }

}
add_action( 'save_post_tribe_events', 'set_to_pending', 10, 3 );

However, when the post is published, it gives an error that "publishing failed" – which I guess is because the post is, in fact, not "published" – it’s pending. Here’s an example:
https://www.loom.com/share/dc89baa6f60a440eb7d48f86ccf55f39

Anybody know how I get around this?

0
WilliamAlexander 3 months 0 Answers 17 views 0

Leave an answer