Very short trick: Wordpress, exclude pages from the search and post

Wednesday, July 8, 2009

The engine inside of WordPress used for research (the standard without the addition of plugins) makes in a classic low-level query_post() The search is carried dapperttutto, without excluding anything. This feature can be controlled, however, allowing, for example, to conduct a search on all but in particular categories, pages or posts. There are several ways to "filter" the standard search of WordPress, one is sfruutando filter pre_get_posts

PHP:
  1. / **
  2. * Enter this code in the file functions.php
  3. * Exclude from the search pages / posts with id 26.27 and 32
  4. * /
  5. $query ) { search_filter function ($ query) (
  6. $query -> is_search ) { if ($ query -> is_search) (
  7. set ( 'post__not_in' , array ( 26 , 27 , 32 ) ) ; $ query -> set ( 'post__not_in', array (26, 27, 32));
  8. )
  9. ; return $ query;
  10. )
  11. , 'search_filter' ) ; add_filter ( 'pre_get_posts', 'search_filter');

This filter is interesting and allows you to perform various activities on our query. Analyzing the input parameter $query you may have an idea on what you can do:

PHP:
  1. $query ) { search_filter function ($ query) (
  2. $query -> is_search ) { if ($ query -> is_search) (
  3. ; echo '<pre>';
  4. $query ) ; print_r ($ query);
  5. ; echo '</ pre>';
  6. )
  7. ; return $ query;
  8. )
  9. , 'search_filter' ) ; add_filter ( 'pre_get_posts', 'search_filter');

For example, you can avoid searching specific categories:

PHP:
  1. / **
  2. * Enter this code in the file functions.php
  3. * Excludes the category with id 14
  4. * /
  5. $query ) { search_filter function ($ query) (
  6. $query -> is_search ) { if ($ query -> is_search) (
  7. set ( 'cat' , '-14' ) ; $ query -> set ( 'cat', '-14');
  8. )
  9. ; return $ query;
  10. )
  11. , 'search_filter' ) ; add_filter ( 'pre_get_posts', 'search_filter');

The first if the code that determines how we are, in this case search This means that we can apply this filter in other contexts, such as:

PHP:
  1. / **
  2. * Enter this code in the file functions.php
  3. * Excludes the category with id 14 from Feed
  4. * /
  5. $query ) { search_filter function ($ query) (
  6. $query -> is_feed ) { if ($ query -> is_feed) (
  7. set ( 'cat' , '-14' ) ; $ query -> set ( 'cat', '-14');
  8. )
  9. ; return $ query;
  10. )
  11. , 'search_filter' ) ; add_filter ( 'pre_get_posts', 'search_filter');

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

One comment to "Very short trick: Wordpress, exclude from the search pages and post"

  1. getAvatar 1.0
    08 lug, 2009 Undolog.com "Very short trick: Wordpress, exclude pages and post ...:

    [...] Further consult original article: Undolog.com "Very short trick: Wordpress, exclude pages and post ... Related articles: Very short trick: parent page and child pages in WordPress [...]

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL