Very short trick: WordPress, excluded from the search pages and post

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');

3 comments to "Very short trick: WordPress, excluded from the search pages and post"

  1. July 8, 2009 Undolog.com "Very short trick: Wordpress, exclude pages and post ... :

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

  2. October 9, 2011 Matt :

    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?

  3. October 10, 2011 Giovambattista Fazioli :

    @ 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.php the following code:

    1
    2
    3
    4
    5
    6
    7
    8
    $where ) { exclude_post_content function ($ where) {
    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:

    1
    2
    3
    4
    5
    6
    7
    $query ) { search_filter function ($ query) {
    $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');

Leave a comment

XHTML TAG PERMIT: <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 [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA