Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there an efficient way to change a post status from 'published' to 'draft' when a user of a certain type tries to update their posts. I tried something along these lines

function change_post_status($post_id)
{
  if(current_user_can('rolename'))
  {
    $current_post = get_post( $post_id, 'ARRAY_A' );
    $current_post['post_status'] = 'draft';
    wp_update_post($current_post);
  }
}

add_action('pre_post_update','change_post_status'); 

The code looks good to me but for some reason it doesnt work properly and I think it creates an endless loop(forcing me to restart my SQL server).

share|improve this question
You get endless loop because your hook calls wp_update_post() which calls wp_insert_post() which calls your hook... And the circle have closed. – Rarst Oct 26 '12 at 21:57

3 Answers

Have you tried to use "wp_insert_post_data" instead of "pre_post_update"?

share|improve this answer
something like this: add_filter('wp_insert_post_data', 'change_post_status', 99, 2); – Daniel Oct 26 '12 at 21:27
I have not, I will give it a shot. Never used that filter before. Not sure how to use it without $post_id as a parameter. – tyler Oct 29 '12 at 15:02

Since your logic is based on role, just not give it publish_posts capability? The way native Contributor role works.

share|improve this answer
This would work, however the way it is set up I made custom post types and the user posts listings via gravity forms. Once their listing is accepted an admin publishes it so after that point the user can update whenever they want. I need it so when they update the post becomes a draft until it is reviewed again. – tyler Oct 29 '12 at 15:01
up vote 0 down vote accepted

So I ended up using the wp_insert_post_data filter and came up with the following, which after testing, seems to be working properly.

add_filter('wp_insert_post_data', 'change_post_status', '99');

function change_post_status($data)
{
    if( (current_user_can('role')) && ($data['post_type'] == 'custom_post_type') )
    {
        if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
        //then set the fields you want to update
        $data['post_status'] = 'draft';     
    }
    return $data;
}
share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.