WordPress makes managing comments easy to do when logged into the WordPress admin, but what if you want to manage your comments using a command line interface? In this article, we'll show you how to manage comments within your WordPress site using WP-CLI.
Showing All Comments
Using WP-CLI, you can easily list all comments on your WordPress site. Simply navigate to your site's root directory and use the following command:
pagely-wp comment list
Showing All Comments for a Specific Post
If you have a lot of comments, you're probably better off with only showing comments for a specific post. To do so, you can use the --post_id argument like this:
pagely-wp comment list --post_id=123
Changing the Comment List Fields
If you need to further filter your comment listing to only include specific fields, you can use the --fields argument, followed by a comma-separated list of fields that you want to show.
pagely-wp comment list --fields=comment_ID,comment_post_ID,comment_content
When filtering by fields, these comment fields are available:
- comment_ID: The comment ID.
- comment_post_ID: The ID of the post that the comment is associated with.
- comment_date: The date/time of the comment.
- comment_approved: If the comment has been approved.
- comment_author: The comment author.
- comment_author_email: The comment author's email.
The following fields may also be available, depending on the particular comment:
- comment_author_url: The comment author's URL.
- comment_author_IP: The comment author's IP.
- comment_date_gmt: The date/time of the comment in GMT.
- comment_content: The content of the comment.
- comment_karma: The karma associated with the comment. Usually used by anti-spam plugins like Akismet.
- comment_agent: The user agent of the author when the comment was posted.
- comment_type: The type of comment that was posted (comment, trackback, pingback).
- comment_parent: The comment parent, if the comment is a reply to another comment.
- user_id: The comment author's user ID, if the author is a registered user.
- url: The comment's URL where it can be viewed.
Getting a Single Comment
The get information about an individual comment, you can use the following command, replacing 123 with your comment ID:
pagely-wp comment get 123
Approving/Unapproving Comments
Approving a Comment
Need to approve a comment? Just grab the comment ID and use the following command from within your WordPress site's root directory:
pagely-wp comment approve 123
Unapproving a Comment
Here's how to unapprove a comment that has already been approved:
pagely-wp comment unapprove 123
Marking a Comment as Spam
If you want to mark a comment as spam, just grab the comment ID and use the following command from within your WordPress site's root directory:
pagely-wp comment spam 123
Trashing Comments
To move a comment to the trash, use the comment's ID within the following command:
pagely-wp comment trash 123
Permanently Deleting Comments
The permanently delete a comment without first placing it into the trash, you can issue the following WP-CLI command:
pagely-wp comment delete 123
Updating Comments
When updating comments with WP-CLI, you'll need to add arguments containing your updated information. Here's a list of the available fields:
- comment_ID: The comment ID.
- comment_post_ID: The ID of the post that the comment is associated with.
- comment_date: The date/time of the comment.
- comment_approved: If the comment has been approved.
- comment_author: The comment author.
- comment_author_email: The comment author's email.
- comment_author_url: The comment author's URL.
- comment_author_IP: The comment author's IP.
- comment_date_gmt: The date/time of the comment in GMT.
- comment_content: The content of the comment.
- comment_karma: The karma associated with the comment. Usually used by anti-spam plugins like Akismet.
- comment_agent: The user agent of the author when the comment was posted.
- comment_type: The type of comment that was posted (comment, trackback, pingback).
- comment_parent: The comment parent, if the comment is a reply to another comment.
- user_id: The comment author's user ID, if the author is a registered user.
- url: The comment's URL where it can be viewed.
For example, if you wanted to update the content for the comment with ID 123 to display "Updated comment content", you would use the following command:
pagely-wp comment update 123 --comment_content='Updated comment content'
Further Reading
For more information on the various WP-CLI commands available for managing WordPress comments, take a look at the official documentation for the wp comment command.