This problem is kind of two part. I have the solution to exclude posts from the search and limit the results to 10 results per page but the problem is it also affects the backend wp-admin area. So if i search for posts through the wp-admin area the posts will be excluded even though I only want this to occur for users who are not admin. This is the code I have for excluding posts and limiting the results to 10:
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
function change_wp_search_size($query) {
if ( $query->is_search ) // Make sure it is a search page
$query->query_vars['posts_per_page'] = 10;
return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');
Now I have done a little bit of reading and research on stack and other sites and it looks like there is a way to only trigger these functions only after checking if admin is logged in. I'll reference to the wordpress codex for the is_admin function
This is the way I attempted to use the is_admin function which caused an http error 500. Please excuse me if the code is grossly incorrect:
if ( !is_admin() ) {
function SearchFilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
if ( !is_admin() ) {
function change_wp_search_size($query) {
if ( $query->is_search && !is_admin() )
$query->query_vars['posts_per_page'] = 10;
return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');
I'm hoping that I'm close and just need some tweaking for the code
Edit: So now this is what i have for both functions. I know its my php code which is the problem can you help further please:
function SearchFilter($query) {
if ( is_admin () ) {
return $query;
}
else {
($query->is_search) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
function change_wp_search_size($query) {
if ( is_admin () ) {
return $query;
}
if ( $query->is_search ) // Make sure it is a search page
$query->query_vars['posts_per_page'] = 10;
return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');
EDIT 2: Pastebin of the entire function.php file http://pastebin.com/tTEnHGp2
function SearchFilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
function change_wp_search_size($query) {
if ( $query->is_search && !is_admin() ) {
$query->query_vars['posts_per_page'] = 10;
}
return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');
This Should do it
Okay don't wrap your search functions around the is_admin() instead put the is_admin() inside the function
Like this:
Function search(){
if ( is_admin()){
//Search query for admin
}
else {
//Search query for users
}
}
That should do it