简体   繁体   中英

Wordpress: How to remove pages from search results

I've got a Wordpress based website and I'd just like to remove pages from search results. Of course I can't just do something like if( is_page() ) { // don't do anything; } if( is_page() ) { // don't do anything; } because it would still count as a result and so it would affect pagination etc...

So, maybe some parameter in the search query? But the thing is, the search query is written in the wordpress core so I can just edit it?

If you mean from the internal wordpress search, there are some plugins that allow you to set a page or article as excluded from search result. You could try this one: https://wordpress.org/plugins/search-exclude/

If you also wan't to exclude the page from the Search engines, you should also add the url of the page to the robots.txt file in your wordpress folder.

I had this exact problem, and I found this piece from John Parris that solved it.

function jp_search_filter( $query ) {
  if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
    $query->set( 'post__not_in', array( 10,11,20,105 ) );
  }
}
add_action( 'pre_get_posts', 'jp_search_filter' );

You can, of course, use a plugin as well. I just had specific need to remove only two products from search and not have them private.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM