最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Prevent A Specific Custom Post Type Showing In WP Search Results Page

programmeradmin0浏览0评论

I've excluded certain pages from the WP Search with the code below in my functions.php file.

I also need to exclude a custom post type called 'sites'.

I can't seem to find the custom post type equivalent of the post__not_in argument ?

I don't want to remove it in the custom post type code itself under public => false because I still want it to have its own url etc.

Does anyone know how to remove a specific custom post type from showing up in WP Search?

Any help would be wonderful !

// EXCLUDE CERTAIN PAGES FROM SEARCH RESULTS (PAGE IDs IN ARRAY)

function tp_remove_pages( $query ) {
  if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
    $query->set( 'post__not_in', array( 11,84,118,115,123,132 ) );
  }
}
add_action( 'pre_get_posts', 'tp_remove_pages' );

I've excluded certain pages from the WP Search with the code below in my functions.php file.

I also need to exclude a custom post type called 'sites'.

I can't seem to find the custom post type equivalent of the post__not_in argument ?

I don't want to remove it in the custom post type code itself under public => false because I still want it to have its own url etc.

Does anyone know how to remove a specific custom post type from showing up in WP Search?

Any help would be wonderful !

// EXCLUDE CERTAIN PAGES FROM SEARCH RESULTS (PAGE IDs IN ARRAY)

function tp_remove_pages( $query ) {
  if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
    $query->set( 'post__not_in', array( 11,84,118,115,123,132 ) );
  }
}
add_action( 'pre_get_posts', 'tp_remove_pages' );
Share Improve this question asked May 31, 2020 at 2:37 pjk_okpjk_ok 9082 gold badges15 silver badges36 bronze badges 1
  • check stackoverflow/questions/39836785/… – Michael Commented May 31, 2020 at 3:21
Add a comment  | 

1 Answer 1

Reset to default 1

Instead of enumerating page IDs you don't want, you could instead set the post types that you do want.

You can of course combine the 2 approaches if you wish, but below I've included pages, posts, and another type you might want if you have other types on your site.

e.g.:

function tp_remove_pages( $query ) {
  if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
    $query->set( 'post_type', array( 'page', 'post', 'other_custom_type_you_want' ) );
  }
}
add_action( 'pre_get_posts', 'tp_remove_pages' );

EDIT: An alternative to using this code to modify the search behaviour "after the fact", is to change the nature of your custom post types when you register them. To do this, use the exclude_from_search argument when registering your post type.

Ref: https://developer.wordpress/reference/functions/register_post_type/

You could also set the public argument to false to achieve a similar result, though this may impact other behaviours of your post type.

发布评论

评论列表(0)

  1. 暂无评论