WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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');
share|improve this answer
    
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 Mar 19 '11 at 22:50
    
You do know that you have a select dropdown just for that in the edit screen? – Bainternet Mar 20 '11 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 Mar 20 '11 at 1:53
    
update your question with the coed you have and i'll get a better view of where i can help. – Bainternet Mar 20 '11 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 Mar 20 '11 at 2:11

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.

share|improve this answer
    
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 Nov 10 '15 at 19:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.