R
R
Ruslan Makarov2016-02-17 22:16:50
WordPress
Ruslan Makarov, 2016-02-17 22:16:50

How to exclude pages from WordPress search without breaking the admin panel?

Good!
I'm using the "Posts" type for the main content, and I want to exclude "Pages" from the search.
There is a great code for this:

// Удаляем с поиска страницы
function searchExcludePages($query) {
 if ($query->is_search) {
  $query->set('post_type', 'post');
 }
return $query;
 }
add_filter('pre_get_posts','searchExcludePages');

It works great on the site... BUT it breaks the admin functionality:
I'm linking a post to another custom post type.
And now the search is broken ...
00088455588342a0a6eb325f0a48c50d.pngQUESTION:
How can I make this code work only on the site and not affect the admin panel?

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mr Crabbz, 2016-02-17
@facepook

function searchExcludePages($query) {
  if(!is_admin()){
    if ($query->is_search) {
      $query->set('post_type', 'post');
    }
  }
  return $query;
}
add_filter('pre_get_posts','searchExcludePages');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question