Home » Create and Edit WordPress posts via wp-cli

Create and Edit WordPress posts via wp-cli

How to create, edit or delete posts in wordpress with WP-CLI

The wp-cli or WordPress command line interface is a unique tool you can use to achieve several tasks related to wordpress from managing wordpress installation, installing and activating plugins, managing updates, managing cache and several more tasks including approving, unapproving and deleting comments on your website. You can even manage your database and clear transients from the wp-cli without any need for a plugin to do that.

Install WP-CLI (WordPress Command Line Interface)

All that you need is to install the wp-cli on your server. You can do that by following the official method. Connect to your server via ssh.

Download wpcli.phar using wget or curl:

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

Verify that it works:

$ php wp-cli.phar --info

Make the file executable and move it into your PATH so that you can run commands by typing wp only.

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

To verify that the WP-CLI was installed successfully, run the following command:

$ wp --info

You will get an output that looks something like the output shown below. I am running wordpress on an EC2 instance and you can see it provides information about the OS, php version, database server and WP-CLI with the same command.


OS: Linux 5.10.167-147.601.amzn2.x86_64 #1 SMP Tue Feb 14 21:50:23 UTC 2023 x86_64

Shell:  /bin/bash

PHP binary: /usr/bin/php

PHP version: 8.1.14

php.ini used:   /etc/php.ini

MySQL binary:   /usr/bin/mysql

MySQL version:  mysql  Ver 15.1 Distrib 10.5.18-MariaDB, for Linux (x86_64) using  EditLine wrapper

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:   /home/ec2-user

WP-CLI packages dir:

WP-CLI cache dir:   /home/ec2-user/.wp-cli/cache

WP-CLI global config:

WP-CLI project config:

WP-CLI version: 2.7.1

The above output shows you have successfully installed the WP-CLI.

Suggested : A list of Useful WP-CLI commands

Create, edit and update wordpress posts via WP-CLI

With a simple command, you can create a draft post with a title in your wordpress installation using ssh. To create the draft, you will need to provide the post title and wordpress directory path (root folder). Run the following command to create a draft post with title ‘My post draft’:

$ wp post create --post_type=post --post_title='My post draft' --path=/var/www/html

The output will say ‘Success’ and provide you a post id. Something like the following:

Success: Created post 17.

If you want to delete the same post from the WP-CLI, you can do it using the ID of the post and wp post delete command in the following manner.

$ wp post delete 17 --path=/var/www/html

The output will be:- Success: Trashed post 17.

Now, what to do if you want to check out a list of already published posts. You can run the wp post list command to get a list of published posts. The command also provides post id, published date, post name, post title and publication status of the post in your wordpress blog.

Take a look at the sample output below:

[ec2-user] $ wp post list –path=/var/www/html


| ID | post_title                | post_name                 | post_date       | post_status |


| 17 | My post draft             |                           | 2023-03-18 16:40:55 | draft   |

| 16 | A sample post             |                           | 2023-03-18 16:28:03 | draft   |

| 14 | Integer sit amet orci lacinia | integer-sit-amet-orci-lacinia | 2023-03-18 06:40:21 | publish |

| 12 | Mauris at blandit est     | mauris-at-blandit-est     | 2023-03-18 06:39:00 | publish |

| 10 | Cras eu nisl lorem        | cras-eu-nisl-lorem        | 2023-03-18 06:38:10 | publish |

| 8  | Sed iaculis volutpat nibh | sed-iaculis-volutpat-nibh | 2023-03-18 06:36:39 | publish |

| 5  | Lorem ipsum dolor sit amet | lorem-ipsum-dolor-sit-amet | 2023-03-18 06:35:15 | publish |

| 1  | Hello world!              | hello-world               | 2023-03-17 13:01:31 | publish |


If I want to limit the output to only post title, I can do it with the following command:

[ec2-user]$ wp post list –path=/var/www/html –fields=post_title

In the below output, you can see that the information is limited to only post title. You can also list the ids of posts using the same command and replacing post_title with ID.


| post_title                |


| My post draft             |

| A sample post             |

| Integer sit amet orci lacinia |

| Mauris at blandit est     |

| Cras eu nisl lorem        |

| Sed iaculis volutpat nibh |

| Lorem ipsum dolor sit amet |

| Hello world!              |


If you want to delete a particular post from the list of posts published or those in draft mode, you will just need the ID of the post and you can delete it without logging into your wordpress dashboard. Just as you created a post with the WP-CLI, you can find out a particular post’s ID and then delete it using the cli.

$ wp post delete 17 --path=/var/www/html

This will trash the post. However, it will not delete the post entirely since the post is still in the trash. To skip the trash and delete the post, you can add –force to the command:

$wp post delete 17 --force --path=/var/www/html

Not just this, you can also add content to your post and publish it via the wp-cli.  Suppose, you have written two paragraphs and want to create a post fast. You have already created a draft with a title. Now, to add the content to the draft post, run the following post edit command with post ID:

$ wp post edit 16 --path=/var/www/html

This will open the editor for inserting content. Type I to enter the insert mode. You can paste the content here, check it and then hit escape to come out of the insert mode. Then type :wq to exit the editor. When you exit, you will see the output :

Success: Updated post 16.

Now, you have added content to your post but not published it yet. You can publish the post from the cli with the command:

$ wp post update 16 --path=/var/www/html --post_status=publish

The above command will publish the post or update the status of the post from draft to publish. You can use the same command to unpublish a post. The default value for post_status is draft.

Create new user and new category via WP-CLI

Suppose, you published a post under the default category. However, you wanted to publish it under another category and wanted to assign it a different author. So, you can also do this while changing its status from draft to publish or later using the post update command like in the following manner.

I created a random post titled ‘A sample post’ and added some random content to it using post edit:

Content of the post:

“This is a sample post. Thanks for reading!

Hi this is a sample post. It’s a lovely day. Hope you are enjoying your day.

Some of our tasks are complete by now. I am going to edit this post using the WP-CLI.

WP-CLI stands for WordPress command line interface which is a useful tool for managing wordpress.”

The post is still in draft and I want to publish it under the category education and by author John McLain. So, first edit the post using

 $ wp post edit 16 --path=/var/www/html.

It opens up the editor. Hit ‘i’ to enter the insert mode and insert the content. Following that hit escape to come out of insert mode and :wq to exit. The output will be – success, updated post 16.  Now, I will publish the same post using post update and with desired changes. However, first I need to make sure I have added the author and the category or the command will not work.

First I will create a user and assign the role author to him. The user name is john and the full name of the author is John Mclain.

$ wp user create john john01@gmail.com --first_name=John --last_name=Mclain --role=author --path=/var/www/html

The output will include user id and a randomly generated password for the user you just added like below:

Success: Created user 2.

Password: t8Zxxxx)P*@zQ@VZxxxvo)x

Here the system has generated a password for the user john we created. If you are adding a user using wp-cli, remember to safely store the username and password.

Now that I have added the author, I will add the category to which I want to publish the post.  I want to create a category education which I can do using the wp term create command like below:

$ wp term create category education --description='Posts on education' --path=/var/www/html

Output: Success: Created category 3.

Now, I will finally publish the post with the new author and in the new category:

$ wp post update 16 --post_status=publish --post_category=education --post_author=2 --path=/var/www/html

If I want to change the author back to the default author or the admin:

$ wp post update 16 --post_author=1 --path=/var/www/html

There is another post with post ID 14, to which I want to assign the new author.  To do that run the following command:

$ wp post update 14 --post_author=2 --path=/var/www/html

You can change the post author of a post using the post id and the author id.

If you want to list all the users on your blog:

$ wp user list --path=/var/www/html


| ID | user_login | display_name | user_email      | user_registered | roles     |


| 1  | blogadmin  | blogadmin | admin@gmail.com | 2023-03-17 | administrator |

| 2  | john   | John Mclain  | john01@gmail.com| 2023-03-19  | author  |


However, the above list is too descriptive. We only need the user names and Id. Suppose, you have several authors on your blog and you want to assign one of them to a post, but do not know the author id, you can generate a list of authors with only ID and display_name using the following command:

$  wp user list --fields=ID,display_name --path=/var/www/html

The output now only includes the display names and ID of the users.


| ID | display_name |


| 1  | blogadmin |

| 2  | John Mclain  |


The above list includes all users and to limit it to authors and exclude other roles like subscribers, use the following :

$  wp user list --role=author --fields=ID,display_name --path=/var/www/html

It will produce the list of authors on your blog and their IDs and display names. Now you can pick the Id of any author and assign it to the post.


Another way to publish a post to wordpress is by creating a txt file and including it in wp post create command. First, we will create the .txt file with the content of our post

$ touch wp-post.txt

$ sudo nano wp-post.txt

Paste the content of your post in this file. (Added sample content for test: This is a sample wordpress post created using the WP-CLI. WordPress Command Line Interface is a great tool to manage your wordpress website.)

Now, let’s run the command to publish the content of wp-post.txt to our wordpress site using wp-cli.

$ wp post create ./wp-post.txt --post_title='A sample wordpress post' --post_status=publish--post_category=education --post_author=2 --path=/var/www/aim-blog/html

In the above command, we begin with the wp cli post create command and then add the content file we have created in .txt format and then add the remain details like title, category and author, with the wordpress path in the end. 

Adding excerpts to posts via WP-CLI

So, we have created a post and assigned an author and a category. Now, we need to add an excerpt for it, which will show in the archives pages and on the home page showing the latest posts. The excerpt can also work as a metadescription. If you are using yoast seo, then in the post settings, insert the variable excerpt.

The command to add or edit excerpt for a post is the post update command with the –post_excerpt field. Check the command below, I have used to add an excerpt to my latest post :

$ wp post update 16 --post_excerpt='how to use the wpcli to create and edit posts' --path=/var/www/html

Output: –

Success: Updated post 16.

Once done, just check out the post on the home page or on the category archives page. You will see the excerpt there and in search results too, it will show the excerpt as description. In this way, you can add excerpts to several posts without the need to go to each post individually and using the same command. All you need is the post id and then just type out a nice excerpt for your post. So, rather than the first few lines, you can show an excerpt for you posts on the home page and archive pages. These excerpts can also be used as meta descriptions.


Just change the directory to the root directory with the following command before running wp cli commands.

$ cd /var/www/html

Now, you can run commands from the root directory and there will be no need to add the path to the root folder at the end of each wp-cli command.

Manage comments in WordPress with the help of WP-CLI