1

A WP user with the role "Author" can post articles. On the blog in question I have the requirement, that these users' articles have to be live immediately but not publicly visible (i.e., for anonymous visitors or Subscribers). We use WP 3.0.5.

We already have a plugin running, that allows to hide categories from anonymous and Subscribers. So the most straight-forward method I came up with so far is: New blog posts by Authors should be automatically put in a category. Then I hide that category from anonymous users.

Does anyone know:

a) how to automatically put an article by "Author" users in a certain category, or

b) how the requirement "live but not public" could be achieved more elegantly for those posts?

(Plugin suggestions are welcome, too.)

Boldewyn
  • 81,211
  • 44
  • 156
  • 212
  • I'm not totally understanding why the 'pending review' status wouldn't suit your needs - it's not visible to logged out users or subscribers and publishable by an administrator or editor. – Gavin Mar 14 '11 at 21:15
  • Because an article is then not visible to the other authors as well. The customer wants instant gratification for authors in that they see their published article just the other minute, and it might be desirable that several authors discuss over a new article, before it goes "really live". I can, by the way, understand that line of thought and find it quite interesting. – Boldewyn Mar 15 '11 at 07:56
  • Hm.. potential alternate approach, although you've already found your answer: Change permissions. Allow authors to view other authors' posts. I *believe* [this](http://wordpress.org/extend/plugins/user-role-editor/) plugin handles that... – Gavin Mar 15 '11 at 16:06
  • @Gavin: That'd be a nice idea, too. I keep that in mind, if the requirements change. Thanks. – Boldewyn Mar 16 '11 at 09:33

2 Answers2

1

What you probably want to do is write function to do this in your theme's functions.php file, and then use add_action to trigger that function when a post is saved.

For example:

function update_category_on_save($post_id) {
    // Get post
    $post = wp_get_single_post($post_id)
    // Map author IDs to category IDs
    $mapping = array(
        1 => array(123),
        2 => array(234),
    );
    // Update the post
    $new_category = $mapping[$post->post_author];
    $u_post = array();
    $u_post['ID'] = $post_id;
    $u_post['post_category'] = $new_category;
    // Only update if category changed
    if($post->post_category != $new_category[0]) {
        wp_update_post($u_post);
    }
}

add_action('category_save_pre', 'update_category_on_save');

Hope that makes sense, and gives you a hint as to how to do this — I'm afraid I haven't been able to test it.

Sam Starling
  • 5,298
  • 3
  • 35
  • 52
  • I've made a small edit - looks like you need to pass an array when changing categories, not just an integer. – Sam Starling Mar 15 '11 at 08:04
  • OK, I ran into a problem. It seems, that when I run wp_update_post from within a save_post hook, I start a recursion loop. Can I use other action hooks for that? – Boldewyn Mar 15 '11 at 10:30
  • OK, that was the starter I needed. Actually, I went with the `category_save_pre` filter hook, since it doesn't require messing around with the whole post. I award the bounty in 6 hours, when I'm allowed to do so (I just learned about the 24 hours limit). – Boldewyn Mar 15 '11 at 11:11
  • Thanks - I've updated the answer to reflect that new filter hook you're using, in case anyone else wants to do this. Glad I could help! – Sam Starling Mar 15 '11 at 11:26
0

The following code will change posts made by an author to private automatically.

function change_author_posts_to_private( $post_status ) {
    // if the user is just saving a draft, we want to keep it a draft
    if ( $post_status == 'draft' )
        return $post_status;

    $this_user = new WP_User( $_POST[ 'post_author' ] );

    // this is assuming the user has just one role, which is standard
    if ( $this_user->roles[0] == 'author' )
        return 'private';
    else    
        return $post_status;
}
add_filter( 'status_save_pre', 'change_author_posts_to_private' );

it filters the status on the post save, looks to see who the author is from the post variables, fetches their first role and sees if it's author, if it is, then it returns 'private', otherwise it returns the natural status. No need to use categories to do it when you can do it directly here.

dwenaus
  • 3,206
  • 2
  • 27
  • 27
  • As I answered to Gavin's comment above, the status doesn't work for me, since other authors should see the article as well. And the category is, apart from that, a fixed requirement for better overviews for all admins and authors. – Boldewyn Mar 15 '11 at 07:58