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.

Read More

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.

Related posts

Leave a Reply

2 comments

  1. Here is a function that changes post status

    /*
    $post_id - The ID of the post you'd like to change.
    $status -  The post status publish|pending|draft|private|static|object|attachment|inherit|future|trash.
    */
    function change_post_status($post_id,$status){
        $current_post = get_post( $post_id, 'ARRAY_A' );
        $current_post['post_status'] = $status;
        wp_update_post($current_post);
    }
    

    simple call the function and pass the post id and the new status you want it to have for example:

    change_post_status(12,'private');