How to remove a page from appearing in my website's search results.
-
Hello
I have created a page which has all the information regarding payments and i don’t want this page to show up when someone types “Payment Methods(My page title)” in my website’s search box. Before publishing it, i checked “NOARCHIVE this page/post:” and “noindex, nofollow” and all other checkboxes but when i type the query in my website’s search box it is still showing up. I am using robots.txt file to stop it from getting indexed in search engines but how to remove this page from my website’s search results.
Regards
Vaibhav Bathla
Hmmm….
This function will basically exclude pages (but not posts) in your search results:
function graphene_exclude_pages($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','graphene_exclude_pages');Alternatively, this function will allow you to exclude any post or page by using it’s ID:
function graphene_search_filter($query) {
if ( !$query->is_admin && $query->is_search) {
$query->set('post__not_in', array(40, 9)); // id of page or post
}
return $query;
}
add_filter('pre_get_posts', 'graphene_search_filter');Just change the (40), and the (9) to whatever posts/pages you want to exclude. Remember to keep them comma-separated if excluding more than one.
Hi Josh
Thanks for the prompt reply. I copied your second code in my child theme’s function.php file but after clicking on the update button it showed a syntax error. After that i also wrote <?php in the starting and ?> in the end but that also didn’t worked. What mistake am i making??
Regards
Vaibhav Bathla
Try this plugin – WP Hide Post : http://wordpress.org/extend/plugins/wp-hide-post/
Viewing 4 posts - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.
