• Resolved srdriggers

    (@srdriggers)


    Anyone know of a function that will automatically mark any post in a certain category as “Private”? Some of my users are having issues remembering to click the “Private” on the post edit screen. If I could handle this by functions.php versus a plugin that would be awesome.

    Basically

    if (cat(44)) {
    // mark private
    }

    Thanks in advance

Viewing 5 replies - 1 through 5 (of 5 total)
  • You could look into Membership Plugins such as Paid Memberships Pro that set categories to private based on membership level.

    Thread Starter srdriggers

    (@srdriggers)

    Thank you @brett but I am really looking for a straight forward functions.php solution. A plugin is overkill for one category.

    Hello srdriggers,

    Took me a little bit, but I think this will work for you. I believe I got snagged on an infinite loop issue while I was working on your function. In theory, the status should change to Private when the user Publishes or Updates their post. Pre-existing posts in the “private” category just need to be updated and their status will also change. Just change the category, switcheroo, to whatever category you are targeting in the function.

    Please give it a try and see if it works.

    function post_saved_set_to_private ( $post_id ){
    	if ( ! wp_is_post_revision( $post_id ) ){
    
    		// unhook this function so it doesn't loop infinitely
    		remove_action('save_post', 'post_saved_set_to_private');
    
    		if ( in_category( 'switcheroo' )) {
    			// Update post
    		    $my_post = array( 'ID' => $post_id, 'post_status' => 'private' );
    
    			// Update the post into the database
    			wp_update_post( $my_post );
    		}
    
    		// re-hook this function
    		add_action('save_post', 'post_saved_set_to_private');
    	}
    }
    add_action('save_post', 'post_saved_set_to_private');
    Thread Starter srdriggers

    (@srdriggers)

    @shulte

    Works great thank you!

    Thread Starter srdriggers

    (@srdriggers)

    Closing this one out. Thanks for all the help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Mark All Posts in a Category private using functions.php?’ is closed to new replies.