Hide wordpress password protected pages

A lot of the time it is not desirable to have a password protected post show up on your WordPress home page or recent posts list however it is the default behaviour.

This code is almost straight out of the manual, however it may help someone looking for a complete example with the plugin header/comment that is required for the file to be recognised as a plugin by WordPress.

<?php
/**
 * @package hpp
 * @version 1.0
 */
/*
Plugin Name: HidePasswordProtected
Plugin URI: http://hotchipsnsource.com/
Description: As per name
Author: hot chips n source
Version: 1.6
Author URI: http://hotchipsnsource.com/
*/
// Filter to hide protected posts
function exclude_protected($where) {
	global $wpdb;
	return $where .= " AND {$wpdb->posts}.post_password = '' ";
}

// Decide where to display them
function exclude_protected_action($query) {
	if( !is_single() && !is_page() && !is_admin() ) {
		add_filter( 'posts_where', 'exclude_protected' );
	}
}

// Action to queue the filter at the right time
add_action('pre_get_posts', 'exclude_protected_action');

?>

Save the code in a file named HidePasswordProtected.php and save it to your wp-content/plugins directory. Then just activate it from within the WordPress Plugins administration page.

There is one flaw with this, while viewing a single post if the next or previous post made was a hidden post the next and previous buttons will expose a link to a hidden page. I have not had time to dig into this as yet and as the posts are not actually accessible it’s not presently a priority. If you know the solution please leave a comment below.

Alternatively there are plugins available on the WordPress Plugins Directory, the ones I tried also suffered from the next and previous issue.

Resurrection

I’ve bought back this site after a long break, piecing together old posts from information I could find in the internet archives. Not everything made it, some of the older information has broken links and some posts needed tweaking. Despite any issues, I’m happy to once again have a place to share and save some of the things I get up to.