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('[email protected]', '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?

Solutions

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 );

Similar questions

Pending post is not showing in front end but showing in admin portal in wordpress
I am submitting a custom post type post from front end wordpress with pending status. I am trying to retrieve the pending post in the front end but it was not showing. I checked the same in admin portal, the pending post is showing there. 1. Able to post the custom post type with pending status using the below code. 2. Not able to show the pending ...
allow editor/author to publish others posts but not publish their own
Im trying to disallow editors from publishing their own post, I would like editors to only be allowed to publish others submitted posts. I would like contributors and authors to submit their posts for review. I would like the editor to approve these posts for publishing, but I do not want the editor to be able to publish their own posts. I have bee...
Allow Editors to edit pending posts but not draft ones
I have a large number of users with Editor Capabilities that help to go through the post submissions. This is my current setup for this role: As you can see, they are allowed to edit_posts and edit_others_posts but they cannot edit_published_posts. This means that they can edit posts that are in Draft and Pending status. Now, I want to restrict the...
Woocommerce : Change order status to pending but do not increase stock
Consider this example: I have a product named "A" and there are 10 available in my store. A user orders and pays for 3. The order status will change to "processing" and the stock reduces. Now, I manually change The order status to "pending". The stock increases back to 10. I want to prevent the increase from happening. This is the code I am using, ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .