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

wordpress.stackexchange https://wordpress.stackexchange.com/questions/377420

  •  21-04-2021
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

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('someone@mail.com', '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 );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top