Blog

How to create a WordPress blog post using WP CLI (remote too)

We can create a WordPress blog post using WordPress CLI (Command line interface) tool: wp-cli
Please follow the installation instructions for your system on this page: https://wp-cli.org/

After we have installed, we can use the wp command now.
We can also use this command to create a blog post even if the blog is on a remote server. For remote we have to provide the -ssh option. Example: wp --allow-root --ssh=root@sites. Please note the –allow-root directive too, which allows us to run wp command even if we are logged in as root user on the system.

Create the new post (we do not publish it yet, this will give us new POST_ID):

wp --allow-root --ssh=root@sites --path=/usr/local/lsws/site/html post create --post_title="A sample post"

Sync post content to remote server (we create a new file on our system and sync it to remote server, using rsync):

rsync -avP "~/Documents/wp-sample-post.txt" sites:/root/remote-wp-post.txt

Update the newly created blog’s content and publish:

wp --allow-root --ssh=root@sites --path=/usr/local/lsws/site/html post update POST_ID --post_status=publish --post_category=XXX --post_author=1 --post_excerpt="This is an awesome post excerpt" /root/remote-wp-post.txt

If we want to find the the existing post, we can use:

wp --allow-root --ssh=root@sites --path=/usr/local/lsws/site/html post list | grep "Sample"

Leave a Reply

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