12

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 Answers 2

29

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.

3
  • Yes, a faster and simpler solution. Plus then it will only worry about writing the update to status and not bother having to re-update or ignore all the other data retrieved in the current post
    – Kirby
    Commented Nov 10, 2015 at 19:37
  • My question is identical to the OP. I am using the code in this answer. The status of my post does not change from draft to publish, but wp_update_post returns 1.
    – TARKUS
    Commented Jun 2, 2021 at 19:29
  • @TARKUS you have to change $status to 'draft'
    – Nathan
    Commented Jul 17, 2021 at 6:58
17

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');
7
  • How would I be able to add the ability make the status an option to select..? I want to be able to toggle between publish and draft only and then update the post with the selected option...
    – user4064
    Commented Mar 19, 2011 at 22:50
  • You do know that you have a select dropdown just for that in the edit screen?
    – Bainternet
    Commented Mar 20, 2011 at 0:15
  • ha ha.. yes.. but I am trying to build a custom admin options page that will avoid that page.. I would like to have a list of specific available pages and give the admin user the ability to just toggle between having it be published.. or as a draft (unpublished) - I know it can be done, but I am struggling with the code to do it.
    – user4064
    Commented Mar 20, 2011 at 1:53
  • update your question with the coed you have and i'll get a better view of where i can help.
    – Bainternet
    Commented Mar 20, 2011 at 1:59
  • I don't have much more code then what you gave me.. I actually dont know how to develop code but I can usually hack something if it is close to what I want to do... Basically I need an options page that allows me to list a specific page id and give the admin the ability to say publish or draft each page listed then hit save (or update) and have it set each page the way the user configured it. I would love to learn how to do this but I feel like I may be asking for a lot in a free support forum.. ?
    – user4064
    Commented Mar 20, 2011 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.