Inline Search Results

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:

https://awsblinds.com/?s=feeders

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’);

Hi @ranchhouse,

You have edited it correctly. Please let us know how it works for you.

Thanks

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:

Best Regards.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.