Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Protected posts won't show up in search results WordPress

  • SOLVED

I'm having a problem when password protecting a post or custom post type: When I'm searching for a protected post within the WP search form it only comes up in the results when logged into Wordpress (admin).

I've tried to use several plugins like Search Unleashed, but not one of them is working. Is it possible to 'hack' the default search engine (core if necessary) to display password protected posts in search results, even when not logged in?

Thanks!

Answers (2)

2011-08-31

Ivaylo Draganov answers:

Hi,

I think you could do that by filtering the query:

function include_password_posts_in_search( $query ) {

if ( is_search() )
$query->set( 'post_status', array ( 'publish', 'private' ) );

}
add_action( 'pre_get_posts', 'include_password_posts_in_search' );


Place the above code in your <em>functions.php</em>. I've just tested it and doesn't work though... I have to dig a little deeper.


Melvin vd Ven comments:

Thank you, shouldn't it be 'protected' instead of 'private'? The page isn't really private but password protected.


Ivaylo Draganov comments:

There's no post status of 'protected' so the above filter applies only to private posts.

You can hack core to include password protected entries. The following lines need to be commented:
[[LINK href="http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/query.php#L2185"]]http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/query.php#L2185[[/LINK]]
[[LINK href="http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/query.php#L2186"]]http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/query.php#L2186[[/LINK]]

That's a little ungraceful though.. There's a filter available but I have to experiment a little to get it working.


Melvin vd Ven comments:

A function for this will be a great solution. Looking forward to it!


Ivaylo Draganov comments:

Please try this filter:

add_filter( 'posts_search', 'include_password_posts_in_search' );

function include_password_posts_in_search( $search ) {

global $wpdb;

$pattern = " AND ({$wpdb->prefix}posts.post_password = '')";

$search = str_replace( $pattern, '', $search );

return $search;

}


Melvin vd Ven comments:

Amazing! Works like a charm. You're the man :-)


Ivaylo Draganov comments:

I just thought that a conditional check could be added, to apply the filter only to not logged in users (cause the logged in ones get the password posts anyway):

add_filter( 'posts_search', 'include_password_posts_in_search' );

function include_password_posts_in_search( $search ) {

global $wpdb;

if( !is_user_logged_in() ) {
$pattern = " AND ({$wpdb->prefix}posts.post_password = '')";
$search = str_replace( $pattern, '', $search );
}

return $search;

}


Melvin vd Ven comments:

Great, added it and is still working when logged in or out. I've voted you the price money, thank you!

2011-08-31

Romel Apuya answers:

what are you using for making the page password protected?

try this one

http://wordpress.org/extend/plugins/page-protection/

it has option to make page searchable even if its password protected.


Melvin vd Ven comments:

Thanks, didn't know this plugin. I will give it a try later on!