WordPress.org

Forums

Changing post status with pre_post_update hook (2 posts)

  1. gregfuller
    Member
    Posted 4 years ago #

    I'm trying to conditionally change the post status with this code:

    // if contributor edits published post, change the status to pending
    function contributor_status_to_pending($post_id) {
    
          $post = get_post($post_id);
    
          if ($post->post_status == 'publish') {
                $post->post_status = 'pending';
         	}
    }
    if ( current_user_can('contributor')  )
        add_action('pre_post_update','contributor_status_to_pending');

    Through debugging, I can see that the function fires for a contributor, that execution gets inside the if, and that $post->post_status is set to 'pending'.

    But the post remains 'publish'.

    Any help would be appreciated

  2. gregfuller
    Member
    Posted 4 years ago #

    I should add that a previous hook was implemented that allowed a contributor to edit a published post, like so:

    / contributor can edit published posts (but leaves published, want to set to pending)
    function allow_contributor_edit_published_posts() {
    	$contributor = get_role('contributor');
    	$contributor->add_cap('edit_published_posts');
    }
    if ( current_user_can('contributor') && !current_user_can('edit_published_posts') )
    	add_action('admin_init', 'allow_contributor_edit_published_posts');

    This code comes before the other hook.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags