UPDATE_POST_META不使用TRANSPATION_COMMENT_STATUS

时间:2020-03-19 作者:Bikram

我正在尝试将平均评论保存为帖子元,每次都需要根据评论状态进行更新。

/**
 * Gets comment average data.
 */

function sw_get_averagerev( $post_id ) {
    if (get_post_type($post_id) == \'review\') {

    $prev_averating = get_post_meta( $post_id, \'wp_review_comments_rating_value\', true );
    $prev_count = get_post_meta( $post_id, \'wp_review_comments_rating_count\', true );

        $comments = get_comments(
                    array(
                        \'post_id\' => $post->ID,
                        \'type\'    => \'wp_review_comment\',
                        \'status\'  => \'approve\',
                    ));

    $ratings_sum = 0;

    foreach ( $comments as $comment ){
        $ratings_sum+= get_comment_meta($comment->comment_ID, \'wp_review_comment_rating\', true);
    };

        $count  = count( $comments );
        $ave_reviews = $ratings_sum / $count;

        $averating = round($ave_reviews , 2);


        update_post_meta( $post_id, \'wp_review_comments_rating_value\', $averating, $prev_averating );
        update_post_meta( $post_id, \'wp_review_comments_rating_count\', $count, $prev_count );
}}


/**
 * Runs something when comment status change.
 */
function wp_review_on_change_comment_status( $new_status, $old_status, $comment="comment_type=wp_review_comment" ) {
     sw_get_averagerev( $post_id ); 
}
add_action( \'transition_comment_status\', \'wp_review_on_change_comment_status\', 10, 3 );
$comment="comment_type=wp_review_comment" 在上可能不正确function wp_review_on_change_comment_status( $new_status, $old_status, $comment="comment_type=wp_review_comment" ). 所以,我也检查了defalt$注释参数,它仍然是一样的。

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

您在中有输入错误$post->ID 由于您没有$post变量,而且它确实可能会让您觉得它不起作用,至少它不会计算新的值。这是一种改进的方法。

我还使用了不同的钩子来触发真正必要的操作,而不是针对每个comment\\u状态转换。

/**
 * Gets comment average data.
 *
 * @param int $post_id
 */
function sw_update_average_review_rating( $post_id ) {

    if ( get_post_type( $post_id ) === \'review\' ) {

        $prev_averating = get_post_meta( $post_id, \'wp_review_comments_rating_value\', true );
        $prev_count     = get_post_meta( $post_id, \'wp_review_comments_rating_count\', true );

        $comments = get_comments(
            array(
                \'post_id\' => $post_id, // HERE was a typo error, you dont\' have here a $post you have $post_id!
                \'type\'    => \'wp_review_comment\',
                \'status\'  => \'approve\',
            )
        );

        $ratings_sum = 0;

        foreach ( $comments as $comment ) {
            $ratings_sum += get_comment_meta( $comment->comment_ID, \'wp_review_comment_rating\', true );
        };

        $count       = count( $comments );
        $ave_reviews = $ratings_sum / $count;

        $averating = round( $ave_reviews, 2 );

        update_post_meta( $post_id, \'wp_review_comments_rating_value\', $averating, $prev_averating );
        update_post_meta( $post_id, \'wp_review_comments_rating_count\', $count, $prev_count );
    }

}

/**
 * Hook action for to trigger recalculation after comment status got changed.
 *
 * @param $comment_id
 * @param WP_Comment $comment
 */
function sw_review_after_comment_status_changed( $comment_id, $comment ) {

    $post_id = $comment->comment_post_ID;
    $post    = get_post( $post_id );
    if ( \'review\' === $post->post_type ) {
        // trigger calculation.
        sw_update_average_review_rating( $post_id );
    }

}

add_action(\'deleted_comment\', \'sw_review_after_comment_status_changed\', 10, 2);
add_action( \'trashed_comment\', \'sw_review_after_comment_status_changed\', 10, 2);

// hooks below can be tuned only to handle your custom comment type. 
add_action( \'comment_approved_\', \'sw_review_after_comment_status_changed\', 10, 2);
add_action( \'comment_unapproved_\', \'sw_review_after_comment_status_changed\', 10, 2);

// Tuned for your custom comment type to trigger only for them, use either these or pair of hooks above.
add_action( \'comment_approved_wp_review_comment\', \'sw_review_after_comment_status_changed\', 10, 2);
add_action( \'comment_unapproved_wp_review_comment\', \'sw_review_after_comment_status_changed\', 10, 2);

相关推荐

Comments.php保留评论日期/时间,但删除日期/时间的#超级链接

我在谷歌上搜索了这个问题,似乎找不到解决方案。。。在评论中,我试图从评论日期/时间中删除超链接,当您将鼠标悬停在评论日期上方时,它会将超链接(示例/#comment-210)链接到以下评论。。。我可以在函数中输入什么。php删除链接,我想保留日期/时间文本。。