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

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('<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d1e0200080203082d000c0401430e0200">[email protected]</a>', '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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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('<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5625393b33393833163b373f3a7835393b">[email protected]</a>', '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 );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x