WordPress.org

Ready to get started?Download WordPress

Forums

Is there a good way to change a post status when users update posts (1 post)

  1. tyler1987
    Member
    Posted 1 year ago #

    I wonder if there is a way to change a post status from 'published' to 'draft' when a user of a certain type tries to update their posts. I am dealing with custom roles and post types so I want it to be a function invoked via the functions.php with an edit_post hook. I tried something along these lines

    function set_status($post_id)
    {
    if ( current_user_can( 'rolename' ) )
       {
          $my_post = array();
          $my_post['ID'] = $post_id;
          $my_post['post_status'] = 'draft';
          wp_update_post( $my_post );
       }
    }
    
    add_action('edit_post','set_status');

    The code looks good to me but for some reason it doesnt work properly and it slows the server down. When a user of a certain role type hits update on their post the function does change their status to "draft" BUT when the administrator goes to publish it the status changes to "published" and then "draft" when refreshed. Any suggestions?

Topic Closed

This topic has been closed to new replies.

About this Topic