Managing Wordpress Posts Via WP-CLI

Page content

WP-CLI

Managing Posts in Wordpress using WP CLI

WP CLI is an excellent tool for managing Wordpress. From managing posts to themes and plugins as well as comments and other aspects of wordpress including updating key files like Wp-conig.php are all easily possible using the WP CLI or Wordpress Command Line Interface.

All you need is a live wordpress site and the Wp CLI installed on your server to help you manage wordpress tasks. You just need to be able to run terminal commands since most WP-CLI commands are simple and easy to run.

You can publish posts and edit existing posts in Wordpress using the WP CLI with the same ease as you do from the post editor. The only things you need to be careful about are the heading tags and the rest of the task is very easy.

To start creating and editing posts from the terminal using the WP-CLI, you need to have the CLI installed and configured on your server. Here is a detailed post to help you with that.

Publishing posts using WP-CLI:

To publish a post directly from the terminal, you will first need to SSH to your server and then open the directory in which Wordpress is installed or the root directory. You can run WP-CLI commands from here.

$ cd /var/www/html

Once you have changed to the root directory, you will not need to add the –path to each WP-CLI command.

Now, we will create a new post with a new title and assign it a category. The status of the new post will be ‘draft’ initially since we have not added content to it yet. It will require us to run the wp post create command.

Run the command from the root directory:

root@wordpress:/var/www/html# wp post create --post_title="My post title" --post_category=3 

Its output will look like -

Success: Created post 456.

This command will create a draft post with the given title and assign it to the category whose id you have provided. If you are unaware of the category id that you need to select for the given post, you can easily list all the categories on your blog and assign the right one from the list. To list all the categories on your blog, run the command:

wp term list category

The output will include all categories and their names and ids in the format of a table. Now you can pick the right category from the list.

Now that you have created a post, you will need to add content to it before you can publish it. To add content to the post, you must edit the post using the wp post edit command.

You will need to add the id of the post you just created from the previous output.

wp post edit 456

It will open the Vim editor for editing the post. Press I to enter Insert mode.

You can first create your post using a text editor or simply start typing inside the system editor. Use the following format to create your article.

<div>

<h2>My first heading</h2>

Here goes the content….
…….

Lines of content………

<h3> My second heading</h3>

More content here……………….

…………………….

</div>

After having added your content, you can exit the Vim editor by typing : wq.

Your article is saved but it is still in draft mode.

Now, you want to publish it and to publish it you need to run the wp post update command to change its status from draft to published.

Run the following command from the root folder :

wp post update –post_status=publish 

Your post has been published and you can check it by going to your website and looking among the latest articles.

In case, you want to delete the same post using the WP-CLI, you can do it using the wp post delete command:

wp post delete 456 --force

The above command will delete the post permanently without moving it to trash. If you do not want to delete the post permanently, do not use the –force flag.

To get the details of a published post, you will need the post id. Run the command:

wp post get 456

The above command will output the details including the content, category, author, post status,guid and other details. If you want, you can save the contents of the post to a .txt file.

 wp post get 456 --field=content > post456.txt

The WP-CLI can also be used to make changes to a post like changing author and published date, changing status from draft to published or vice versa.

For example, if you want to change the author of a specific post or multiple posts, you can update the post author using the command wp post update asin the following command.

wp post update 123 456 --post_author=15

To change the comment status on a specific post from open to closed:

 wp post update 456 --comment_status=closed --ping-status=closed

The above command will close comments and pingbacks for the specified post.

Remember that you must run the WP-CLI commands from the root folder of your website where Wordpress is installed.

Otherwise, append the –path=/path/to/root_folder to the command. The CLI would throw an error if you are not running the commands from the root directory or did not append the path to root directory to the command.