Search results limit to certain pages

Hi there

Is it possible to limit the search to search for page titles only and only certain pages involved?

Background: We will have sites like

  • about us
  • support

but also a lot of different airlines

  • Lufthansa
  • Swiss
  • Austrian
    and so on

on the frontend I would like to have a search field like “search for your airline” and show then the the airlines we have pages for.

thanks
Andreas

Hi Andreas,

Thanks for reaching out.
There is no such option to set only a few pages for search, but you can achieve that by adding the following code into your child theme functions.php file.
The following code will set the specific page id(s) when you search on your site, and the result will come up from those pages only.

function wpb_search_filter( $query ) 
{
    if ( $query->is_search && !is_admin() )
    {
        $query->set('post_type', array( 'page' ) );  
        $query->set('post__in', array( '1594' ) ); // add your page id(s)
    }
    return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

Remember that the above code will work if copied as it is and don’t conflict with any existing style.
Please note that the code provided serves only as a guide to help you get started custom coding on your own if there’s no option offered in our theme or the products we bundle.
We do not provide support for custom codes that means we can’t fix it in case it conflicts with something in your site nor will we enhance it.

Note: Please find the article to locate the post or page id(s): https://theme.co/docs/how-to-locate-post-ids

Thanks

thanks a lot for your help!

Hi Andreas,

You’re welcome and it’s our pleasure to help you. If you have any other concerns, feel free to reach us.

Thank you.

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