Skip to content

Back

Managing WordPress posts using WP-CLI

Applies to:
WordPress Hosting

Difficulty:
Easy

Time Needed:
10 minutes

Introduction

This is a quick guide to using WP-CLI to manage and perform maintenance on your WordPress blog posts.

WP-CLI is a command line tool for managing your WordPress website. You can use this guide to get started.


Backing up your website

Take a Snapshot

Before running code that edits your posts, you should take a backup of your website. Our Snapshot guide shows you how to do this.

Taking a snapshot in the 34SP.com control panel

Basic post management

Listing your posts

The command to list all posts on your site is:

wp post list --post_type=post
Listing posts using WP-CLI

Create a post

The command to create a draft post on your site is:

wp post create --post_type=post --post_title='A test post' --post_status=draft
Creating posts using WP-CLI

Delete a post

You will need the ID number of the post from wp post list in order to delete a post, the command to delete a post on your site is:

wp post delete <ID>

You need to replace <ID> with the ID number of your post.

Deleting posts using WP-CLI

Searching post specifics

Find posts by author

The command to search posts by a specific author is:

wp post list --post_type=post --author=<USERNAME>

You need to update <USERNAME> with the username of the author you want to search for.

Listing posts using WP-CLI by author

Find draft posts

The command to search for all draft posts is:

wp post list --post-type=post --post_status=draft
Listing draft posts using WP-CLI

Find published posts

The command to search for all published posts is:

wp post list --post-type=post --post_status=published
Listing published posts using WP-CLI

Basic post management

Changing a post status

You will need the ID number of the post from wp post list in order to update a post’s status.

The command to set a post to draft is:

wp post update <ID> --post_status=draft

The command to set a post to published is:

wp post update <ID> --post_status=published

You need to replace <ID> with the ID number of your post.

Setting posts to draft using WP-CLI
Setting posts to published using WP-CLI

Toggle comment status

You will need the ID number of the post from wp post list in order to change comment status.

The command to disable comments on a post is:

wp post update <ID> --comment_status=closed

The command to enable comments on a post is:

wp post update <ID> --comment_status=open

You need to replace <ID> with the ID number of your post.

Setting post comment status to closed using WP-CLI
Setting post comment status to open using WP-CLI

Toggle ping status

You will need the ID number of the post from wp post list in order to change ping status.

The command to disable comments on a post is:

wp post update <ID> --ping_status=closed

The command to enable comments on a post is:

wp post update <ID> --ping_status=open

You need to replace <ID> with the ID number of your post.

Setting post ping status to closed using WP-CLI
Setting post ping status to open using WP-CLI

Add a tag to a post

You will need the ID number of the post from wp post list in order to add post tags.

The command to add a tag to a post is:

wp post term add <ID> post_tag <TAG>

You need to replace <ID> with the ID number of your post, and <TAG> with the word you wish to add to the tag list.

Setting post tags using WP-CLI

Change a post category

You will need the ID number of the post from wp post list in order to assign post categories.

The command to assign a post category:

wp post update <ID> --post_category=<CATEGORY>

You need to replace <ID> with the ID number of your post, and <CATEGORY> with the name, or ID of your post category.

Setting post category using WP-CLI

Post range management

Disable comments for ALL posts

The command to disable comments for all posts is:

wp post list --format=ids | xargs wp post update --comment_status=closed

The command to enable comments for all posts is:

wp post list --format=ids | xargs wp post update --comment_status=open
Disabling all post comments using WP-CLI
Enabling all post comments using WP-CLI

Advanced post queries

The following commands make use of wp db query to perform complex database queries on your posts. Be sure to take a snapshot before using any of these.

Set posts to draft within date range

The command for setting a post range to drafts is:

wp post update $(wp post list --post__in=$(wp db query 'SELECT ID FROM wp_posts WHERE post_date>="2014-01-01" AND post_date<="2014-01-02";' --skip-column-names | paste -s -d ',' -) --format=ids) --post_status=draft

You will need to update ‘2014-01-01’ to be the start date you want posts to be edited FROM, and ‘2014-02-01’ should be changed to the date you want posts to be edited TO, both in YYYY-DD-MM format.

You may also need to update wp_posts to match your table prefix if you are using a custom prefix.

Setting draft posts within a date range using WP-CLI

List posts without featured images

The command to find all posts without featured images is:

wp db query "SELECT DISTINCT ID, post_title FROM wp_posts WHERE post_type='post' AND ID NOT IN (SELECT post_id FROM wp_postmeta WHERE meta_key='_thumbnail_id')"

You may need to edit this query if you are using a custom table prefix.

Listing posts without featured images using WP-CLI

List posts with featured images

The command to find all posts without featured images is:

wp db query "SELECT DISTINCT ID, post_title FROM wp_posts WHERE post_type='post' AND ID IN (SELECT post_id FROM wp_postmeta WHERE meta_key='_thumbnail_id')"

You may need to edit this query if you are using a custom table prefix.

Listing posts using featured images in WP-CLI

List posts without a Yoast keyword set

This command only works on websites using Yoast.

The command to find all posts without a keyword set is:

wp db query "SELECT DISTINCT ID, post_title FROM wp_posts WHERE post_type='post' AND ID NOT IN (SELECT post_id FROM wp_postmeta WHERE meta_key='_yoast_wpseo_focuskw')"

You may need to edit this query if you are using a custom table prefix.

Listing posts with a Yoast keyword set using WP-CLI
Was this article helpful?
YesNo