Change Visibility to Private

时间:2016-10-17 作者:David

我想知道在提交评论后,是否有办法将帖子的状态更改为私人状态。更具体地说,3条注释,我已经编写了一些代码,并希望在其中添加此函数。我是否有可能以某种方式将其挂接到这段代码中?

    global $post,$current_user;
$args = array( \'post_id\' => $post->ID );
$comment = get_comments( $args );
  get_currentuserinfo();
  if ($post->post_author == $current_user->ID && 3 <= count( $comment ) ){
    echo do_shortcode( \'[button]\' );
} elseif ( 3 <= count( $comment ) ) {
//blank
} else {
    comment_form();
}

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

你非常接近。您只想在保存评论的正确时间加入。这是未经测试的,但应该有效。

add_action( \'comment_post\', \'wpse_make_private_after_3_comments\', 10, 2 );

function wpse_make_private_after_3_comments( $comment_ID, $comment_approved ) {
    $comment = get_comment( $comment_ID );
    $post_ID = $comment->comment_post_ID;
    $comments = wp_count_comments( $post_ID );

    // You could also access approved, moderated, spam or trashed comments
    // from the return object of wp_count_comments().

    $comment_count = $comments->total_comments;

    // If we only have 1 or 2 comments, we\'ll bail early
    if ( $comment_count < 3 ) {
        return;
    }

    $post_data = array(
        \'ID\' => $post_ID,
        \'post_status\' => \'private\'
    );

    wp_update_post( $post_data );

    // You might want to add a wp_redirect() here to 
    // so people don\'t automatically see a 404 page 
    // when the comment saving is complete since the page will be private.
}

相关推荐

Set a taxonomy as private

我创建了分类增值税,并将public设置为false。但在前端,我可以通过…/?增值税=22。如何保留该分类法供内部使用?下面是我为此分类法创建的代码。add_action( \'init\', \'gp_register_taxonomy_vat\' ); function gp_register_taxonomy_vat() { $labels = array( \'name\' => \'VAT\', \