Use wpcli and Cron to Unpublish WordPress page

Posted in Web Development on 06.10.2015 by @chasebadkids

The Situation

Have you ever found yourself in a situation where you need to automatically unpublish a specific blog post or page in WordPress? Maybe you need to take down a contest late at night, remove a blog article or some other reason. Recently I found myself in this position and decided to put together a little one liner using the wpcli to make it happen. Automate All The Things!

The Code

How it works

Cron Jobs

If you’re not familiar with cron scheduling syntax, check out this tool

The wpcli

In order to use the code above, you will also need to have the wp-cli binary installed locally on your system. It doesn’t necessarily need to be installed globally, but it will need to be accessible by whichever user you are planning on executing the script as. You can read more about the wp-cli at their site

Essentially, all you will need to do is make sure the path to your wp binary is fully qualified as the $PATH that cron uses can vary. You will also need to change the ID of the post or page you’re looking to edit as well as the path to your actual WordPress installation.

The Power of the wpcli

You can probably come up with a lot of other valuable ways to use this power. The wpcli gives you lots of power over WordPress. You could do things like automatically purge the WordPress cache, update WordPress core, update WordPress plugins, update WordPress themes, you can even use it to run the actual wpcron() itself! The wpcli even gives you the ability to import data and export data automatically. You could use some sort of WebService or API to dynamically populate pages or posts.

In the future I will publish more articles that relate to the power of the wpcli and how it can be used to make your day to day management of WordPress more efficient.

this is a test