Exclude custom article types from WordPress search results

WordPress comment five hundred and fifty-five Reading mode

If you do not want WordPress to customize the article type, you can use the following code to display it in the search results.

 Exclude custom article types from WordPress search results

Exclude custom article types from search results

Add code to the current topic function template functions.php:

 function zm_filter_search( $query ) { if ( ! $query->is_admin && $query->is_search ) { //Add the article type you want to display in the search results $query->set( 'post_type', array( 'post', 'page', 'book' ) ); } return $query; } add_filter( 'pre_get_posts', 'zm_filter_search' );

Book is a user-defined article type. You can delete a page if you do not want the page to be displayed in the search results.

Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!

 weinxin
My Wechat
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 anonymous

Comment

Anonymous netizens
 :?:  :razz:  :sad:  :evil:  :!:  :smile:  :oops:  :grin:  :eek:  :shock:  :???:  :cool:  :lol:  :mad:  :twisted:  :roll:  :wink:  :idea:  :arrow:  :neutral:  :cry:  :mrgreen:

Drag the slider to complete validation