wp_update_comment_count_now

Advertisement

Summery Summery

Updates the comment count for the post.

Syntax Syntax

wp_update_comment_count_now( int $post_id )

Parameters Parameters

$post_id

(Required) Post ID

Return Return

(bool) True on success, false if the post does not exist.

Source Source

File: wp-includes/comment.php

	 * @since 4.7.0
	 * @since 5.5.0 Returning a WP_Error value from the filter will short-circuit comment update
	 *              and allow skipping further processing.
	 *
	 * @param array|WP_Error $data       The new, processed comment data, or WP_Error.
	 * @param array          $comment    The old, unslashed comment data.
	 * @param array          $commentarr The new, raw comment data.
	 */
	$data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr );

	// Do not carry on on failure.
	if ( is_wp_error( $data ) ) {
		if ( $wp_error ) {
			return $data;
		} else {
			return false;
		}
	}

	$keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id', 'comment_agent', 'comment_author_IP' );
	$data = wp_array_slice_assoc( $data, $keys );

	$rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );

	if ( false === $rval ) {
		if ( $wp_error ) {
			return new WP_Error( 'db_update_error', __( 'Could not update comment in the database.' ), $wpdb->last_error );
		} else {
			return false;
		}
	}

	// If metadata is provided, store it.
	if ( isset( $commentarr['comment_meta'] ) && is_array( $commentarr['comment_meta'] ) ) {
		foreach ( $commentarr['comment_meta'] as $meta_key => $meta_value ) {
			update_comment_meta( $comment_ID, $meta_key, $meta_value );
		}
	}

	clean_comment_cache( $comment_ID );
	wp_update_comment_count( $comment_post_ID );

	/**
	 * Fires immediately after a comment is updated in the database.
	 *
	 * The hook also fires immediately before comment status transition hooks are fired.
	 *
	 * @since 1.2.0
	 * @since 4.6.0 Added the `$data` parameter.
	 *
	 * @param int   $comment_ID The comment ID.
	 * @param array $data       Comment data.
	 */
	do_action( 'edit_comment', $comment_ID, $data );

	$comment = get_comment( $comment_ID );

Advertisement

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.

Advertisement

Leave a Reply