How do I search for only the specified article types in wordpress? Learned on the http://www.wpbeginner.comThe Hook "pre_get_posts" method provided by WP may be implementedThis hook method allows you to process the query criteria before querying the database, placing the following code in the functin.php of the topic to search for only the article content
/**
* [只对指定的类型进行搜索]
* @param [type] $query [搜索的参数]
*/
function SearchFilter($query) {
//仅搜索时
if ($query->is_search) {
//设定指定的文章类型,这里仅搜索文章
$query->set(‘post_type‘, ‘post‘);
//指定文章和自定义类型
//$query->set(‘post_type‘, array(‘post‘, ‘custom-post-type‘));
//排除指定的文章ID号
//$query-->set( ‘post__not_in‘, array( 10,11,20,105 ) );
//搜索指定的类型
//$query->set(‘cat‘,‘8,15‘);
//搜索条件....
}
return $query;
}
add_filter(‘pre_get_posts‘,‘SearchFilter‘);
Reference: WordPress Plugin api/action Reference/pre Get posts
How to Exclude Pages from WordPress Search Results
Exclude specific pages, articles, and categories from WordPress search results
From for notes (Wiz)
How WordPress searches for article content without searching page pages