Hello, we have an inline search feature set up on this website. Is there a way to have the results only display pages and not blog posts? Even though we don’t have any blog posts, it seems to be pulling up these posts before it displays pages:
Hi @ranchhouse,
Thanks for reaching out.
There is no such option to set only the pages for search, but you can achieve that by adding the code mentioned in the following thread into your child theme functions.php file.
You need to change the post_type parameter to page to get the search only from Pages.
Hope it helps.
Thanks
Hi, thanks so much for your help. I tried adding that in the functions.php file, but it still brings up these blog post type results. I don’t understand why those come up at all because we actually haven’t created any blog posts. This is the code I added to the bottom of the functions.php file under Additional Functions. Did I edit it correctly?
function JustPostFilter($query)
{
if ($query->is_search && !current_user_can(‘administrator’) )
{
$query->set(‘post_type’, ‘page’);
}
return $query;
}
add_filter(‘pre_get_posts’,‘JustPostFilter’);
It didn’t work. Any other suggestions?
Hello @ranchhouse,
You can test this sample code instead:
// Remove Posts from search
function JustPostFilter($query){
if ( $query->is_search && !is_admin() ) {
$query->set('post_type', '-post');
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','JustPostFilter');
The code above serves as an example code. Feel free to modify it when needed. Please note that custom coding is beyond the scope of our support. You will have to maintain any custom coding to make sure that it will still work after any updates or does not create any issues or incompatibility in the future.
Or perhaps, these articles can help you instead:
- https://www.wpbeginner.com/plugins/how-to-exclude-specific-pages-authors-and-more-from-wordpress-search/
- https://www.greengeeks.com/tutorials/exclude-certain-pages-from-your-wordpress-website-search/
Best Regards.
This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.