Kamis, 16 Februari 2017

04.43
This tip is a quick one but something asked for regularly. By default the WordPress will show both posts and pages in the results.
If you’d rather just show posts and you use the Thesis framework, just place the code below in your custom_functions.php file and you’re done!
?
1
2
3
4
5
6
7
8
9
<?php
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>
Here’s an updated version of this that fixes the search when you’re in the administration screen, basically leaving that to it’s default behaviour when your logged in.
The other thing I’ve added to this is the ability to place a hidden form field in a search form then use this if statement to test if we want to search just with in a custom post type, in this case one called organisations.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function SearchFilter($query) {
if ($query->is_search && !is_admin()) {
 
    // are we wanting to search Custom posts?
    if ($_GET['bsearch'] =="yes"){
            $query->set('post_type', 'organisations');
    }
    else{
            //default behavour we want is to search posts only
            $query->set('post_type', 'post');
    }
 
 
}
return $query;
}
 
add_filter('pre_get_posts','SearchFilter');

0 komentar:

Posting Komentar