The motor inside of WordPress used for research (the standard without the addition of plug-in) performs a classic low-level query_post() . The search is dapperttutto without exclusion. This feature can be controlled, however, allowing, for example, to search all over except in special categories, pages or posts. There are several ways to "filter" the standard search of WordPress, a filter is sfruutando pre_get_posts :
1 2 3 4 5 6 7 8 9 10 11 12 | / ** * Enter this code in the file functions.php * Excludes the search pages / posts with id 26.27 and 32 * / $query ) { search_filter function ($ query) { $query -> is_search ) { if ($ query -> is_search) { set ( 'post__not_in' , array ( 26 , 27 , 32 ) ) ; $ Query -> set ('post__not_in', array (26, 27, 32)); } ; return $ query; } , 'search_filter' ) ; add_filter ('pre_get_posts', 'search_filter'); |
This filter is interesting and allows you to perform various activities on our search query. By analyzing the input parameter $query you may have an idea on what you can do:
1 2 3 4 5 6 7 8 9 10 | $query ) { search_filter function ($ query) { $query -> is_search ) { if ($ query -> is_search) { ; echo '<pre>'; $query ) ; print_r ($ query); ; echo '</ pre>'; } ; return $ query; } , 'search_filter' ) ; add_filter ('pre_get_posts', 'search_filter'); |
For example, you can exclude particular categories of research:
1 2 3 4 5 6 7 8 9 10 11 12 | / ** * 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 in the code that determines how we, in this case search . This means that we can apply this filter in other contexts, such as:
1 2 3 4 5 6 7 8 9 10 11 12 | / ** * 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'); |










[...] See original article further: Undolog.com "Very short trick: WordPress, exclude pages and post ... Related Articles: Very short trick: pages and pages fathers daughters on WordPress [...]
Very interesting. But I would need one more thing: current research also scans the text of the page / post. How can I change it because they do not look inside the text content is limited but for example to search only the title and tag?
@ Matt:: Search in title only, which I know, you can regulate the conditions where generated during the search. If you try to put in the file
functions.phpthe following code:2
3
4
5
6
7
8
is_search ( ) ) { if (is_search ()) {
'/OR\s*\(\w+\.post_content\s+LIKE\s*(\'[^\']+\')\s*\)/' ; $ Pattern = '/ OR \ s * \ (\ w + \. Post_content \ s + LIKE \ s * (\' [^ \ '] + \') \ s * \) / ';
preg_replace ( $pattern , "" , $where ) ; $ Where = preg_replace ($ pattern, "", $ where);
}
; return $ where;
}
, 'exclude_post_content' ) ; add_filter ('posts_where', 'exclude_post_content');
Practically eliminate codizioni research content from running normally select WordPress.
For the 'tag' the issue is similar to examples found nell'artiolo, that will use the filter
pre_get_posts. For example, if you want to search only in the posts tagged 'first-tags', use:2
3
4
5
6
7
$query -> is_search ) { if ($ query -> is_search) {
set ( 'tag' , 'prima-tag' ) ; $ Query -> set ('tags', 'first-tags');
}
; return $ query;
}
, 'search_filter' ) ; add_filter ('pre_get_posts', 'search_filter');