Today I had a request for one of my clients; the request was “I want to change the ownership of all the comments from the user X to Y.”. this
That task can be accomplished via the admin, right? But that user had hundreds of comments going page by page and comment by comments was not an option.
Fortunately, We have WP_CLI to help us with that task.
The first step is to get the comment list from that user.
The command to get the comments is the follow:
wp comment list --author_email=user@email.com
And the output from that is:
+------------+-----------------+---------------------+------------------+----------------+----------------------+ | comment_ID | comment_post_ID | comment_date | comment_approved | comment_author | comment_author_email | +------------+-----------------+---------------------+------------------+----------------+----------------------+ | 220283 | 16 | 2016-10-03 15:01:52 | 1 | UserX | user@email.com | | 220282 | 567 | 2016-10-03 14:56:24 | 1 | UserX | user@email.com | | 220281 | 16 | 2016-10-03 14:52:00 | 1 | UserX | user@email.com | | 220280 | 1204 | 2016-10-03 14:48:26 | 1 | UserX | user@email.com | +------------+-----------------+---------------------+------------------+----------------+----------------------+
Great, We have the list, now to update a comment we need the comment_id according to with the WP_CLI documentation
wp comment update 123 --comment_author='That Guy'
Well, we need to pass the ID from the previous query to this new command, how we can accomplish that?
The need to use the xargs command, this command is available in any *nix environment
xargs is a command on Unix and most Unix-like operating systems used to build and execute command lines from standard input. Commands such as
grep
andawk
can accept the standard input as a parameter, or argument by using a pipe. However, others such ascp
andecho
disregard the standard input stream and rely solely on the arguments found after the command. Additionally, under the Linux kernel before version 2.6.23, and under many other Unix-like systems, arbitrarily long lists of parameters cannot be passed to a command,[1] soxargs
breaks the list of arguments into sublists small enough to be acceptable.
We can use the input, in this case, the comment ID, and update the fields with our new data
wp comment list --author_email=user@email.com --field=ID|xargs -I % wp comment update % --comment_author='newUserAuthor' --comment_author_email='newuser@email.com' --user_id=10
Let’s split that command line:
wp comment list --author_email=user@email.com --field=ID
We get the comment ID from the comments where the author_email is user@email.com
|xargs -I % wp comment update % --comment_author='newUserAuthor' --comment_author_email='newuser@email.com' --user_id=10
Update the comment ID,comment ID and thecomment ID where thecomment ID is “%”, remember that we are populating that “%” with the comment ID that we got in the output.
And that’s it, the output in the console should be something like:
Success: Updated comment 220281. Success: Updated comment 220280. Success: Updated comment 220279. Success: Updated comment 213116. Success: Updated comment 213115.
Leave a Reply