Stephanie Leary

Writer and WordPress consultant

  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Fascism Watch (2016-17)
    • Content Modeling for WordPress series
    • WordPress Hidden Gems series
  • Work
    • Portfolio
    • Services
    • WordPress Plugins
    • WordPress Themes
    • Presentations and Interviews
    • on GitHub →
  • About
    • Press Kit
  • Contact
    • Mailing List

Create private categories

January 16, 2010 Stephanie Leary 4 Comments

Have you ever tried to create a category consisting entirely of private posts? It’s a pain to have to remember to set the visibility for each post before publishing it. One of my users simply couldn’t remember to do it, resulting in a lot of accidentally public posts on that site.

To solve the problem, I created this little function that catches posts as they’re being saved. It checks to see if the post is in the right categories, and if it’s being published — that is, it leaves draft, pending, and scheduled statuses alone. (It’ll automatically catch the scheduled posts when they go live.) If the post is being published, it gets set to private.

Here’s the code you can put in your theme’s functions.php file:

//update_option('private_categories', array(79,34,75,73), ", 'yes');
add_action('save_post', 'set_private_categories');
function set_private_categories($postid) {
	global $wpdb;
	if ($parent_id = wp_is_post_revision($postid))
		$postid = $parent_id;
	$privatecats = get_option('private_categories');
	if (!is_array($privatecats))
		$privatecats = array($privatecats);
	foreach ($privatecats as $cat) {
		if (in_category($cat, $postid)) {
			// wp_update_post() calls the save_post action again and could create an infinite loop, so we'll use the brute force method
			$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET `post_status` = %s WHERE ID = %d AND `post_status` = %s", 'private', (int)$postid, 'publish') );
		}
	}
}

For the sake of simplicity, I haven’t included the code you would need to create an option panel, although you could do so yourself if you like. (I’ll go over it in a later post.) To select your categories using this code alone, uncomment the first line (update_option…). Replace the numbers in the array (79,34,75,73) with the IDs of your private categories. Then view your site. You can view any post or page — on the public side, not from the dashboard — and functions.php will execute, setting your option for you. Then comment out the line again (there’s no need to reset the option every time someone visits your site!) and save functions.php. When you need to add a category, repeat this step.

The private category feature now included in the Private Suite plugin, which includes a number of things related to private and password-protected posts and pages.

WordPress categories, Privacy, private, restriction

Comments

  1. Frank says

    February 7, 2010 at 4:05 am

    Is there any way to retroactively set all posts of a particular category to ‘private’? I know there’s bulk edit, but that only does one page of posts at a time. I have dozens of pages.

    Reply
    • admin says

      February 7, 2010 at 10:33 am

      No, Frank, but I used the screen options tab to increase the number of posts per page to 100, so I had just a few pages to bulk edit.

      Reply
  2. Frank says

    February 7, 2010 at 10:50 am

    Thanks, that’s useful. I love the plug-in!

    Reply
  3. Jon says

    January 15, 2012 at 10:30 pm

    Didn’t work for me, did nothing, until I un-commented the update, then it crashed my functions file..?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Fascism Watch

The Fascism Watch is a daily(ish) news roundup. View all the previous Fascism Watch posts »

Latest WordPress Book

Content Strategy for WordPress

A short book for content strategists and managers on implementing a complete content strategy in WordPress: evaluation, analysis, content modeling, editing and workflows, and long-term planning and maintenance.

Read the sample chapter

Kindle Nook iBooks Kobo Smashwords

WordPress for Web Developers

WordPress for Web Developers (9781430258667)

This is a book for professional web designers and developers who already know HTML and CSS, and want to learn to build sites with WordPress. The book begins with a detailed tour of the administration screens and settings, then digs into server-side topics like performance and security. The second half of the book is devoted to development: learning to build WordPress themes and plugins.

This is the second, much-revised and updated edition of Beginning WordPress 3, with a more accurate title. Everything’s been updated for WordPress 3.6.

WordPress for Web Developers is out now. See what's inside...

The best WordPress features you’ve never noticed

  • WordPress Hidden Gems: Screen Options
  • WordPress Hidden Gems: Bulk Edit
  • WordPress Hidden Gems: Private Status
  • WordPress Hidden Gems: Dashboard Feed Readers
  • WordPress Hidden Gems: Options.php

Content Modeling for WordPress series

  • Content modeling for WordPress, part 1: analyze content
  • Content modeling for WordPress, part 2: functional and organizational requirements
  • Content modeling for WordPress, part 3: a sample content model

This is an excerpt from Content Strategy for WordPress.My latest books are Content Strategy for WordPress (2015) and WordPress for Web Developers (2013). Sign up to be notified when I have a new book for you.

Copyright © 2021 Stephanie Leary