2016/02/22

hide homepage, pages or post from search results wordpress

In your functions.php add the appropriate snippet;


To remove pages from the search results add in…










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

To remove posts from the search results add in…









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

To add in custom post types to the search results add in










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

In the last code snippet we are passing in an array of different post types, just adjust the names of them to suit your needs.

ليست هناك تعليقات:

إرسال تعليق