Applies to:
WordPress Hosting
Difficulty:
Easy
Time Needed:
10 minutes
Contents
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.
Basic post management
Listing your posts
The command to list all posts on your site is:
wp post list --post_type=post
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
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.
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.
Find draft posts
The command to search for all draft posts is:
wp post list --post-type=post --post_status=draft
Find published posts
The command to search for all published posts is:
wp post list --post-type=post --post_status=published
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.
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.
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.
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.
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.
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
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 FROMwp_posts
WHEREpost_date
>="2014-01-01" ANDpost_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.
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.
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.
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.