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
- / **
- * Enter this code in the file functions.php
- * Exclude from the search pages / posts with id 26.27 and 32
- * /
- $query ) { search_filter function ($ query) (
- $query -> is_search ) { if ($ query -> is_search) (
- )
- ; return $ query;
- )
- , '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:
- $query ) { search_filter function ($ query) (
- $query -> is_search ) { if ($ query -> is_search) (
- ; echo '<pre>';
- ; echo '</ pre>';
- )
- ; return $ query;
- )
- , 'search_filter' ) ; add_filter ( 'pre_get_posts', 'search_filter');
For example, you can avoid searching specific categories:
- / **
- * Enter this code in the file functions.php
- * Excludes the category with id 14
- * /
- $query ) { search_filter function ($ query) (
- $query -> is_search ) { if ($ query -> is_search) (
- set ( 'cat' , '-14' ) ; $ query -> set ( 'cat', '-14');
- )
- ; return $ query;
- )
- , '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:
- / **
- * Enter this code in the file functions.php
- * Excludes the category with id 14 from Feed
- * /
- $query ) { search_filter function ($ query) (
- $query -> is_feed ) { if ($ query -> is_feed) (
- set ( 'cat' , '-14' ) ; $ query -> set ( 'cat', '-14');
- )
- ; return $ query;
- )
- , 'search_filter' ) ; add_filter ( 'pre_get_posts', 'search_filter');













[...] 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 [...]