Using WordPress ‘get_comment_statuses()’ PHP function

The get_comment_statuses() WordPress PHP function retrieves all of the WordPress supported comment statuses.

Usage

$comment_statuses = get_comment_statuses();

This example retrieves all supported comment statuses and stores them in the $comment_statuses variable.

Parameters

  • None

More information

See WordPress Developer Resources: get_comment_statuses()

Examples

Display Comment Statuses

This example displays a list of supported comment statuses.

$comment_statuses = get_comment_statuses();

foreach ($comment_statuses as $status => $label) {
    echo "Status: {$status}, Label: {$label}<br>";
}

Count Comments by Status

This example counts the number of comments for each comment status.

$comment_statuses = get_comment_statuses();

foreach ($comment_statuses as $status => $label) {
    $args = array(
        'status' => $status,
        'count' => true
    );
    $count = get_comments($args);
    echo "Number of {$label} comments: {$count}<br>";
}

Check if a Status is Valid

This example checks if a given status is a valid comment status.

$status_to_check = 'approved';
$comment_statuses = get_comment_statuses();

if (array_key_exists($status_to_check, $comment_statuses)) {
    echo "{$status_to_check} is a valid comment status.";
} else {
    echo "{$status_to_check} is not a valid comment status.";
}

Filter Comments by Status

This example retrieves comments with a specific status and displays them.

$status_filter = 'spam';
$comment_statuses = get_comment_statuses();

if (array_key_exists($status_filter, $comment_statuses)) {
    $args = array(
        'status' => $status_filter
    );
    $comments = get_comments($args);

    foreach ($comments as $comment) {
        echo "Comment ID: {$comment->comment_ID}<br>";
    }
} else {
    echo "Invalid comment status.";
}

Add a Custom Status Label

This example adds a custom label to a comment status and displays the updated list of comment statuses.

$comment_statuses = get_comment_statuses();
$custom_status = 'unapproved';
$custom_label = 'Unapproved';

if (!array_key_exists($custom_status, $comment_statuses)) {
    $comment_statuses[$custom_status] = $custom_label;

    foreach ($comment_statuses as $status => $label) {
        echo "Status: {$status}, Label: {$label}<br>";
    }
} else {
    echo "Status {$custom_status} already exists.";
}