How to update page status from publish to draft and draft to publish

I am trying to figure out how to write the code that will allow me to do the following with pages of a WordPress blog.

I need to have something where I specify which page ID’s I want to list (about 15 total) and then give the user the ability to select which ones will be published or which ones will be a draft. This will remove them from the menu and will also remove page from the site as well.

I found this statement

To change a post status, you get the post, change its status field,
then call wp_update_post with the new post object

The closest existing plugin is http://wordpress.org/extend/plugins/wp-hide-pages/ except that this plugin uses wp-list-pages. And, it only hides them and does not actually move them from Publish to Draft.

2

A faster solution is:

$post = array( 'ID' => $post_id, 'post_status' => $status );
wp_update_post($post);

This way you don’t have to get the post.

Leave a Comment