Skip to content

Instantly share code, notes, and snippets.

@nickbosch
Created April 1, 2012 21:12
Show Gist options
  • Save nickbosch/2278863 to your computer and use it in GitHub Desktop.
Save nickbosch/2278863 to your computer and use it in GitHub Desktop.
Limit WordPress search to currently logged in user
<?php
function limit_search_author( $query ) {
if ( $query->is_search ) {
$current_user = wp_get_current_user();
if ( !( $current_user instanceof WP_User ) )
return $query;
if ( 0 != $current_user->ID ) {
$query->set( 'author', $current_user->ID );
}
}
return $query;
}
add_filter( 'pre_get_posts', 'limit_search_author' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment