Manage WordPress Comments Using WP-CLI

The ability to work with your WordPress website(s) from the Command Line Interface (CLI), is one of the many great features available with WordPress Hosting by InMotion Hosting.

For instance, using the WordPress Command Line Interface (WP-CLI) command: wp comment, you can manage comments that visitors have left on your blog posts and pages, directly from the command line. In this guide, you will learn how to view, moderate, edit, and delete WordPress comments using WP-CLI.

View Comments

One of the most important tasks involved in managing your WordPress blog, is viewing the comments that visitors/users write. WP-CLI commands make this process simple while working from the command line. Follow the steps below to learn how to view comments, using the wp comment command.

  1. SSH into your server as the cPanel user that owns the website you want to view WordPress comments on.
  2. Use the cd command to switch to the document root directory of your website.

    Example: cd public_html/my_website

  3. Run the following command:

    wp comment list

    The output of this command by default, will display a table of the WordPress comments for your website, as shown in the example output below:

    wp comment list command example output

     
  4. Using the wp comment list command these fields will be displayed by default for each comment:

    comment_ID
    comment_post_ID
    comment_date
    comment_approved
    comment_author
    comment_author_email

    Although this information is useful, it does not provide the actual content of the comment. However, it does provide the comment_ID, which is needed to view the additional details of each comment. Using the comment_ID with the wp comment command, you can view the content of the comment. This is accomplished by specifying comment_content as the field in your command. The following format is an example of how to do this:

    wp comment get $comment_ID –field=$field

    [email protected] [~/public_html]# wp comment get 1 --field=comment_content Hi, this is a comment. To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard. Commenter avatars come from <a href="https://gravatar.com">Gravatar</a>.

Now that you have reviewed the content of the comment you can moderate it. The next section will continue to use this comment as the example in the instructions to moderate a comment.

Moderate Comments

Moderating comments is important if you have Comment must be manually approved set in WordPress’s Discussion Settings. When enabled, a comment must be approved before it appears on your website. This is a good precaution to take, to avoid allowing visitors to post unwanted/unfiltered content in your comments unknowingly. In this case, you will need to approve the comment prior to it appearing on your website. Simply follow the instructions below to learn how to moderate the comment we viewed in the previous section’s example.

Approve

  1. SSH into your server as the cPanel user that owns the website you would like to moderate WordPress comments on.
  2. Use the cd command to switch to the document root directory of your website.

    Example: cd public_html/my_website

<li>Locate the <em>comment_ID</em> as instructed by the directions in the <a href="#view-comments" target="_blank" title="Click here to review the previous section's instructions to locate the value of comment_ID." rel="noopener">"View Comments" section</a>.</li>
<li>Run the following command:

wp comment approve $comment_ID

[email protected] [~/public_html]# wp comment approve 1
Success: Approved comment 1.

Since the comment has been moderated and approved, it will now appear on your website. However, if you moderate a comment that is clearly Spam, you could mark a comment as Spam, using WP-CLI, by following the steps below.

Mark as Spam

  1. SSH into your server as the cPanel user that owns the website you would like to moderate WordPress comments on.
  2. Use the cd command to switch to the document root directory of your website.

    Example: cd public_html/my_website

<li>Locate the <em>comment_ID</em> as instructed by the directions in the <a href="#view-comments" target="_blank" title="Click here to review the previous section's instructions to locate the value of comment_ID." rel="noopener">"View Comments" section</a>.</li>
<li>Run the following command:

wp comment spam $comment_ID

[email protected] [~/public_html]# wp comment spam 2
Success: Marked as spam comment 2.

Now that you have moderated your comment to mark it as Spam, it will not display in your website. Marking comments as Spam, helps to identify which comments can be deleted, without disrupting the desired comments. However, if you accidentally mark a comment as Spam, you can unmark it. Simply follow the instructions below to learn how to unmark a comment as Spam.

Unmark Spam

  1. SSH into your server as the cPanel user that owns the website you are moderating WordPress comments on.
  2. Use the cd command to switch to the document root directory of your website.

    Example: cd public_html/my_website

  3. Locate the comment_ID as instructed by the directions in the “View Comments” section.
  4. Run the following command:

    wp comment unspam $comment_ID

    [email protected] [~/public_html]# wp comment unspam 2
    Success: Unspammed comment 2.

Edit

Using the wp comment command, you can edit various fields to update comment details. The steps below will guide you through how to edit a comment.

  1. SSH into your server as the cPanel user that owns the website you want to edit WordPress comments on.
  2. Use the cd command to switch to the document root directory of your website.

    Example: cd public_html/my_website

  3. Locate the comment_ID as instructed by the directions in the “View Comments” section.
  4. Run the following command:

    wp comment update $comment_ID$field=$value

    [email protected] [~/public_html]# wp comment update 4 --comment_author=A WP Admin
    Success: Updated comment 4.

Now that you are familiar with updating a comment, you can update any of the available fields using this same method. The available fields are:
comment_ID
comment_post_ID
comment_date
comment_approved
comment_author
comment_author_email
comment_author_url
comment_author_IP
comment_date_gmt
comment_content
comment_karma
comment_agent
comment_type
comment_parent
user_id

Delete

Deleting comments is as simple as editing or listing them. Follow the steps below to learn how to delete a comment using WP-CLI.

  1. SSH into your server as the cPanel user that owns the website you want to delete WordPress comments on.
  2. Use the cd command to switch to the document root directory of your website.

    Example: cd public_html/my_website

  3. Locate the comment_ID as instructed by the directions in the “View Comments” section.
  4. Run the following command:

    wp comment delete $comment_ID

    [email protected] [~/public_html]# wp comment delete 2 
    Success: Trashed comment 2.

Congratulations! You are now familiar with how to moderate comments from the command line, using the wp comment command. To learn more about using WP-CLI, be sure to check out our Education Channel.

InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

Was this article helpful? Join the conversation!