Manage Wordpress Comments using WP-CLI

Page content

Using WP-CLI to manage wordpress comments

The WP-CLI or Wordpress command line interface is a wonderful tool for managing various tasks and actions in wordpress. There are several tasks that can be made a lot easier when done manually through the admin dashboard or through phpmyadmin. WP-CLI allows you to manage the database without going to the phymyadmin interface.

Managing comments in Wordpress is easy and whether you need to approve, unapprove or delete comments, you can do it all from the Wordpress dashboard without any difficulty.

However, the wp-cli can make your task even easier and help with managing comments with a lot of ease with a few simple commands.

You will need to have wp-cli installed on your server to run the WP-CLI commands. WP-CLI commands can help you handle several complicated tasks in wordpress with convenience.

To install the wp-cli, you need to download wp-cli.phar using wget or curl.

 curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Make the file executable and move it to somewhere in your PATH:

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

Now you can run commands starting with wp. To test if the wp-cli was properly installed, run the following command:

wp --info

You will see an output like the following, denoting the command line interface was successfully installed.

OS:     Linux 5.15.0-76-generic #83-Ubuntu SMP Thu Jun 15 19:16:32 UTC 2023 x86_64
Shell:  /bin/bash
PHP binary:     /usr/bin/php8.1
PHP version:    8.1.2-1ubuntu2.14
php.ini used:   /etc/php/8.1/cli/php.ini
MySQL binary:
MySQL version:
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /var/www/html
WP-CLI packages dir:
WP-CLI cache dir:       /root/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.8.1

List comments using WP-CLI:

To simply check out all the comments on your wordpress site, you need to run the comment list command. It will output a list of all the comments on your site with the details including comment ID, author, author email and more.

wp comment list

The output will include all the comments with the following details:

comment_ID
comment_post_ID
Comment_date
Comment_approved
Comment_author
comment_author_email

If you want only the id of comments, you can list their ids by passing the flag –field=ID

$ wp comment list –field=ID

The above command will output only a list of ids of comments on your blog. However, it will output a list of all the comments on your blog. If you do not want all the comments but the comments only on a specific post, you can include the post_id of the post you want to check.

$ wp comment list –post_id=123

The above command will output a list of comments posted on the post_id you have specified in the command with details including comment_ID, comment_post_ID and so on. You can limit these to specific fields with the help of –fields flag.

$ wp comment list --post_id=123 --fields=ID,comment_date,comment_author

Now the output will be limited to the specified fields only.

However, the above command will not output the comment content. If you want to check out the content of each comment, you can add it to the fields as in the command below:

 wp comment list --fields=ID,comment_content

Suppose, you want to get the details of a specific comment including everything like author name, enail id, comment date and comment content, you will only need to run the comment get command:

wp comment get 4

The output will look like the following:

+----------------------+---------------------+
| Field                | Value               |
+----------------------+---------------------+
| comment_ID           | 4                   |
| comment_post_ID      | 0                   |
| comment_author       | sammy               |
| comment_author_email |                     |
| comment_author_url   |                     |
| comment_author_IP    |                     |
| comment_date         | 2023-09-01 15:45:00 |
| comment_date_gmt     | 2023-09-01 15:45:00 |
| comment_content      | hello everyone      |
| comment_karma        | 0                   |
| comment_approved     | 1                   |
| comment_agent        |                     |
| comment_type         | comment             |
| comment_parent       | 0                   |
| user_id              | 0                   |
| url                  | /#comment-4         |
+----------------------+---------------------+

You can limit the output to specific fields when retrieving the details related to a specific comment on your blog.

wp comment get 4 --fields=comment_author,comment_content 

Suppose you want to check out the content of a specific comment using its comment id, you can do that by specifying it using the field flag. The below command will output only the content of the comment id you have specified.

wp comment get 4 --field=content

Now, what if you wanted to get the number of comments on your post including the ones approved, spam and trash comments. You can run the comment count command to get a count of all the comments with status:

wp comment count 

The above command will output the number of comments in the form of a list like the following:

approved:        60
spam:           10
trash:           10
post-trashed:    0
all:             80
moderated:       0
total_comments:  80

If you want to get a count of comments on a single post, then you will just need to append the post_id to the above command.

Create comments using WP-CLI

Just like you list existing comments, you can create new comments on your Wordpress blog using wp-cli.

You can create new comments using the wp comment create command. This command allows you to create comments for specific posts and approve them.

wp comment create --comment_post_ID=12 --comment_content="hello everyone" --comment_author="sammy" –comment_author_email=”abc@gmail.com”

In the above command, we have specified the post_id where we want the comment to be posted and included the content of the comment with content author name and email.

The output will look like the following:

Success: Created comment 4.

If you want to generate dummy contents for a specific post, use the comment generate command instead of comment create command:

wp comment generate --count=3

This command will generate three random comments. You can generate comments for a specific post by using the –post_id flag:

Wp comment generate --count=3 --post_id=345

Approve and Unapprove Comments using WP-CLI

One of the main tasks involved in managing comments is to approve or unapprove them. It can be done through the admin dashboard or using WP CLI.

You can check out the comments on your blog whose status is approved or unapproved using the status flag with wp comment list command.

Wp comment list --status=approve

Or, to list the comments that are unapproved:

Wp comment list --status=unapprove 

To approve comments you will need to list the ids of the comments and use the wp comment approve command.

wp comment approve 123 354 666 789

Using the above command, we approved 4 comments at once with IDs - 123, 354,666 and 789. In this way, you can approve multiple comments using their IDs at the end of the comment approve command.

To unapprove comments, you will simply need to use the wp comment unapprove command:

wp comment unapprove 123 354 666

Its output will look like the following:

Success: Unapproved comment 123.
Success: Unapproved comment 354.
Success: Unapproved comment 666.

Delete comments using WP-CLI

Deleting comments using the wp-cli is easy and you will only need the id of the specific comments that you want to delete. You can run the wp comment delete command to delete comments. To delete multiple comments, you can include multiple comment ids separated by a space.

wp delete comment 1425 3425 --force

You will need to add the force flag whether you are deleting one comment or many.

However, if you want to delete all the spam comments on your blog at a single go, then you will need to run the following command:

wp comment delete $(wp comment list --status=spam --format=ids)

Spam or Unspam a comment

To mark a specific comment as spam, you will need to run the comment spam command followed by the comment id:

wp comment spam 124

The method to mark several comments as spam is also similar. Please run the wp comment spam command followed by the comment ids separated by a space.

wp comment spam 124 336 445

The output of the above command will look like the following:

Success: Marked comment 124 as spam.
Success: Marked comment 336 as spam.
Success: Marked comment 445 as spam.

The method to unmark comments that have been sent to spam folder is not very different. You will only need to replace spam with unspam and you can unmark several comments that have been marked as spam at once:

wp comment unspam 124 336 445

Its output will look like the following:

Success: Unspammed comment 124.
Success: Unspammed comment 336.
Success: Unspammed comment 445.

It is just as simple to mark comments as spam or unspam them using the wp-cli.

Trash or Untrash a comment

To trash a comment or multiple comments, please run the wp comment trash command followed by ID/IDs of the comments to trash.

wp comment trash 123

Or

wp comment trash 123 234 345

The output will look like the following:

Success: Trashed comment 123.
Success: Trashed comment 234.
Success: Trashed comment 345.

You can obtain a list of comments in the trash folder using the following command:

wp comment list --status=trash

Once you have obtained a list of comments in trash, you can untrash the ones you want to. To untrash a comment or multiple comments, you just need the comment ids and run the wp comment untrash command followed by the comment id.

wp comment untrash 123 

Or untrash multiple like in the following command:

wp comment untrash 123 234 345

Update a comment using wp cli

Suppose you need to update a comment like change its author or author email, you can do that using the wp-cli easily. The wp-cli command comment update helps you change these values. For example, if you want to update a comment whose id is 1234, and you want to change its author. You can do that with the following command:

wp comment update --comment_author='sammy'

This will change the comment author from the existing author to the new author you have specified or Sammy in our case. Its output will look like the following:

Success: Updated comment 123.

A few last words

The WP-CLI is a great tool for managing Wordpress. It can be used to conduct almost all the tasks that we perform using the Wordpress dashboard. The inbuilt commenting system in wordpress is great for connecting with your readers and receiving feedback. However, when it comes to to checking out the comments, their content and moderating them, you will not always need to to log in to the Wordpress dashboard.

You can moderate them using the CLI from the terminal. So, next time rather than opening the admin dashboard, just ssh to your server and then check out the comments using the Wordpress command line interface.

Even if spam has been bothering you, it is not a problem anymore. You can bulk moderate comments on your wordpress blog including approving and unapproving comments in bulk, sending them to spam or trash and other similar tasks easily from the terminal.

The WP-CLI can help you in more areas and you could even publish posts in Wordpress using the terminal. From managing themes and plugins to other small and major tasks related to Wordpress or wordpress database can be performed with a lot of convenience from the CLI. Moreover, the CLI commands are very simple and you can handle more complicated Wordpress tasks using the flags.

One key thing to remember when using the CLI is that you must be inside the root directory where your Wordpress installation is located or you will need to include –path=/var/www/html or whatever the path to your root folder is, at the end of each command. Otherwise, the CLI will throw an error. The CLI will also throw an error if you try to use it as root. So, you can switch to another sudo user and then run cli commands or use the –allow-root flag if you want to use it as root.

SUGGESTED READING

MANAGE WORDPRESS POSTS WITH WP-CLI

COMPLETELY DISABLE COMMENTS IN WORDPRESS

A LIST OF THE BEST WORDPRESS CACHING PLUGINS

HOW TO REMOVE PROUDLY POWERED BY WORDPRESS FROM WORDPRESS THEMES

USES OF THE WP-CONFIG.PHP FILE IN WORDPRESS