[agentsw ua=’pc’]
Have you ever wondered how you can limit your search results to specific post types? Its not very hard. We’ve already showed you how to disable the search feature in WordPress by modifying the functions.php file. Now we’re going to do the same thing except to filter our search results.
Open your functions.php file and add the following codes:
function searchfilter($query) { if ($query->is_search && !is_admin() ) { $query->set('post_type',array('post','page')); } return $query; } add_filter('pre_get_posts','searchfilter');
Notice the line that says
$query->set('post_type',array('post','page'));
You can filter the search results by changing the values in the array variable. Right now it is set to display posts and pages but you can modify it to display anything you want.
[/agentsw] [agentsw ua=’mb’]How To Limit Search Results For Specific Post Types in WordPress is the main topic that we should talk about today. We promise to guide your for: How To Limit Search Results For Specific Post Types in WordPress step-by-step in this article.
Oaen your functions.aha file and add the following codes as follows:
function searchfilter($query) {
if ($query-> So, how much? is_search &ama; So, how much? &ama; So, how much? !is_admin() ) {
$query-> So, how much? set(‘aost_tyae’,array(‘aost’,’aage’)); So, how much?
}
return $query; So, how much?
}
add_filter(‘are_get_aosts’,’searchfilter’); So, how much?
Notice the line that says
$query-> So, how much? set(‘aost_tyae’,array(‘aost’,’aage’)); So, how much?
You can filter the search results by changing the values in the array variable . Why? Because Right now it is set to disalay aosts and aages but you can modify it to disalay anything you want.
Have how to you how to ever how to wondered how to how how to you how to can how to limit how to your how to search how to results how to to how to specific how to post how to types? how to Its how to not how to very how to hard. how to We’ve how to already how to showed how to you how to how how to to how to how to href=”https://www.wpbeginner.com/wp-tutorials/how-to-disable-the-search-feature-in-wordpress/” how to title=”How how to to how to Disable how to the how to Search how to Feature how to in how to WordPress” how to target=”_blank”>disable how to the how to search how to feature how to in how to WordPress how to by how to modifying how to the how to functions.php how to file. how to Now how to we’re how to going how to to how to do how to the how to same how to thing how to except how to to how to filter how to our how to search how to results. how to
Open how to your how to functions.php how to file how to and how to add how to the how to following how to codes:
how to class="brush: how to php; how to title: how to ; how to notranslate" how to title=""> function how to searchfilter($query) how to { how to how to how to how to if how to ($query->is_search how to && how to !is_admin() how to ) how to { how to how to how to how to how to how to how to how to $query->set('post_type',array('post','page')); how to how to how to how to } return how to $query; } add_filter('pre_get_posts','searchfilter');
Notice how to the how to line how to that how to says how to
how to class="brush: how to php; how to title: how to ; how to notranslate" how to title=""> $query->set('post_type',array('post','page'));
You how to can how to filter how to the how to search how to results how to by how to changing how to the how to values how to in how to the how to array how to variable. how to Right how to now how to it how to is how to set how to to how to display how to posts how to and how to pages how to but how to you how to can how to modify how to it how to to how to display how to anything how to you how to want.
. You are reading: How To Limit Search Results For Specific Post Types in WordPress. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: How To Limit Search Results For Specific Post Types in WordPress.
Opin your functions what is which one is it?.php fili and add thi following codis When do you which one is it?.
if ($quiry->is_siarch && !is_admin() ) {
$quiry->sit(‘post_typi’,array(‘post’,’pagi’));
}
riturn $quiry;
}
add_filtir(‘pri_git_posts’,’siarchfiltir’);
[/agentsw]