admin管理员组文章数量:1130349
I'm looking to hide from search a few custom post type posts. I'm thinking that if I create a custom taxonomy for the custom post type with a term "hidden" then all posts checked will be hidden. Maybe some filter that I can add to the functions file? By chance has anyone done this or something similar? Please advise, thanks in advance :)
Regards, Kendell
I'm looking to hide from search a few custom post type posts. I'm thinking that if I create a custom taxonomy for the custom post type with a term "hidden" then all posts checked will be hidden. Maybe some filter that I can add to the functions file? By chance has anyone done this or something similar? Please advise, thanks in advance :)
Regards, Kendell
Share Improve this question asked Feb 25, 2018 at 0:28 Kendell DanielKendell Daniel 131 silver badge5 bronze badges2 Answers
Reset to default 2register_post_type() has a argument that you can set to specify which post types you want searched, thus excluded the undesired ones. This is the simplest method, just one line:
'exclude_from_search' = true,
Alternative to that, you can hook into the search query itself via pre_get_posts and then modify the search, specifying which post types you're after:
add_filter('pre_get_posts',function ($query) {
if ($query->is_search && !is_admin() )
$query->set('post_type',array('post','page'));
return $query;
});
If you were dead set on doing the term hiding - although I don't recommend it, as it's more code with a higher chance of user error - you'd do somthing like:
add_action( 'pre_get_posts', function ( $query ) {
global $wp_the_query;
if($query === $wp_the_query && $query->is_search() && !is_admin()) {
$tax_query = array(
array(
'taxonomy' => 'your_custom_tax_name',
'field' => 'slug',
'terms' => 'hidden',
'operator' => 'NOT IN',
)
);
$query->set( 'tax_query', $tax_query );
}
});
For the sake of anyone else searching for a solution i used the SearchWP plugin to exclude these posts.
I'm looking to hide from search a few custom post type posts. I'm thinking that if I create a custom taxonomy for the custom post type with a term "hidden" then all posts checked will be hidden. Maybe some filter that I can add to the functions file? By chance has anyone done this or something similar? Please advise, thanks in advance :)
Regards, Kendell
I'm looking to hide from search a few custom post type posts. I'm thinking that if I create a custom taxonomy for the custom post type with a term "hidden" then all posts checked will be hidden. Maybe some filter that I can add to the functions file? By chance has anyone done this or something similar? Please advise, thanks in advance :)
Regards, Kendell
Share Improve this question asked Feb 25, 2018 at 0:28 Kendell DanielKendell Daniel 131 silver badge5 bronze badges2 Answers
Reset to default 2register_post_type() has a argument that you can set to specify which post types you want searched, thus excluded the undesired ones. This is the simplest method, just one line:
'exclude_from_search' = true,
Alternative to that, you can hook into the search query itself via pre_get_posts and then modify the search, specifying which post types you're after:
add_filter('pre_get_posts',function ($query) {
if ($query->is_search && !is_admin() )
$query->set('post_type',array('post','page'));
return $query;
});
If you were dead set on doing the term hiding - although I don't recommend it, as it's more code with a higher chance of user error - you'd do somthing like:
add_action( 'pre_get_posts', function ( $query ) {
global $wp_the_query;
if($query === $wp_the_query && $query->is_search() && !is_admin()) {
$tax_query = array(
array(
'taxonomy' => 'your_custom_tax_name',
'field' => 'slug',
'terms' => 'hidden',
'operator' => 'NOT IN',
)
);
$query->set( 'tax_query', $tax_query );
}
});
For the sake of anyone else searching for a solution i used the SearchWP plugin to exclude these posts.
本文标签: Hide custom post type from search based on custom taxonomy
版权声明:本文标题:Hide custom post type from search based on custom taxonomy 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749116360a2318263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论