Using WordPress ‘get_pending_comments_num()’ PHP function

The get_pending_comments_num() WordPress PHP function retrieves the number of pending comments on a single post or multiple posts.

Usage

To use the function, simply pass the post ID or an array of post IDs as the argument:

get_pending_comments_num($post_id);

Input:

$post_id = 42;

Output:

3

Parameters

  • $post_id (int|array) – Required. Either a single Post ID or an array of Post IDs.

More information

See WordPress Developer Resources: get_pending_comments_num

Examples

Get pending comments count for a single post

This example retrieves the number of pending comments for a specific post with ID 42.

$post_id = 42;
$pending_comments = get_pending_comments_num($post_id);
echo "Pending comments for post $post_id: $pending_comments";

Get pending comments count for multiple posts

This example retrieves the number of pending comments for multiple posts with IDs 12, 24, and 36.

$post_ids = array(12, 24, 36);
$pending_comments = get_pending_comments_num($post_ids);

foreach ($pending_comments as $post_id => $count) {
    echo "Pending comments for post $post_id: $count<br>";
}

Display pending comments count for all posts in a loop

This example displays the number of pending comments for each post in a WordPress loop.

if (have_posts()) {
    while (have_posts()) {
        the_post();
        $post_id = get_the_ID();
        $pending_comments = get_pending_comments_num($post_id);
        echo "Pending comments for post $post_id: $pending_comments<br>";
    }
}

Display pending comments count for all posts in a category

This example displays the number of pending comments for each post in a specific category with ID 5.

$query = new WP_Query(array('cat' => 5));

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        $post_id = get_the_ID();
        $pending_comments = get_pending_comments_num($post_id);
        echo "Pending comments for post $post_id: $pending_comments<br>";
    }
}

Display pending comments count for all posts by an author

This example displays the number of pending comments for each post by a specific author with ID 7.

$query = new WP_Query(array('author' => 7));

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        $post_id = get_the_ID();
        $pending_comments = get_pending_comments_num($post_id);
        echo "Pending comments for post $post_id: $pending_comments<br>";
    }
}