manage posts with wp-cli

I was recently in the situation where I needed to put all my posts in pending mode, I wanted to review them one by one to be sure they worked well with the block editor and the new style for the site.

From the backend it’s impossible to do such a task, you have bulk actions but they work only on the number of posts which are displayed in a single page, and I had 150 of them.

What I did is fairly simple, I used wp-cli with a little of bash to pull this off. Here’s the script:

#! /bin/bash

WP=$(which wp)

PUBLISHED=$(${WP} post list --post_status=publish --field=ID)

for POST in $PUBLISHED; do
	$WP post update $POST --post_status=pending
done

it’s very simple, I made a list of posts with a post_status of “published” and asked to return only ID’s. I saved this list in a variable and then with a simple for loop I changed the same post_status to “pending” for each post in that list.

It worked like a charm and this just shows how flexible wp-cli is for managing your WordPress site.

I’ll leave you with a link to the project’s site in case you don’t know it and want to find out about it.

remote management

Following from the previous paragraph, what if we don’t want to fire up a full ssh session only to run a few commands on our WordPress install?

wp-cli has the functionality to work remotely, we just need a few setup steps.

  • make sure wp-cli is installed on our remote host and is available as wp
  • configure our local wp-cli install by writing a config.yml inside the hidden .wp-cli directory inside our home.

Here’s an example of .wp-cli/config.yml (only a few options, you can check all the available options inside the handbook).

# always use colors when printing output
color: true

# production server alias https://example.com
@prod:
  ssh: example.com
  user: admin
  path: /var/www/html

# local server
@dev_local:
  path: /var/www/html/wpdanix
  user: admin

as you can see we defined 2 aliases, and now we can call wp directly from our local computer and use it to operate on the remote server like this:

wp @prod transient delete "testing_data"

Of course if we don’t have a session open or a key-agent open we’ll be asked to enter our password/passphrase to connect, and depending on how our ssh server is configured, the session might expire and we might be asked for the password again on the next command, but configuring ssh and ssh-key-agent is widely documented online. Anyway, just like this we deleted the transient “testing_data” from our remote database.

We could also check for updates to our core installation, themes or plugins like this:

wp @prod core update
wp @prod theme update --all
wp @prod plugin update --all

I think it’s awesome in its simplicity. I hope you enjoy using it and, as usual, if you have any questions feel free to ask and I’ll try to help you as much as I can.

Danilo M.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

This site uses Akismet to reduce spam. Learn how your comment data is processed.