在发布时将新帖子更改为“待定”--但“发布失败”--为什么?

时间:2020-10-30 作者:WilliamAlexander

我只想让2名仰慕者能够发布事件。因此,我想将任何其他用户发布的任何新事件设置为;“待定”;当帖子为“时”;已发布"E;我想,从技术上讲,这意味着它不会被出版。但不管怎样,这就是我所尝试的:

   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 );
然而,当帖子发布时,它给出了一个错误,即;发布失败-我想这是因为这篇文章实际上不是;“已发布”正在等待。下面是一个示例:https://www.loom.com/share/dc89baa6f60a440eb7d48f86ccf55f39

有人知道我是怎么解决的吗?

1 个回复
最合适的回答,由SO网友:Paul Kevin 整理而成

这个wp_update_post 钩子将调用同一操作两次,作为save_post_{$post->post_type} 在此函数中调用。我想补充一下remove_action( \'save_post_tribe_events\', \'set_to_pending\'); 之前wp_update_post( $post );add_action( \'save_post_tribe_events\', \'set_to_pending\', 10, 3 ); 工作后:)

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

相关推荐