Limit navbar search to woocommerce?

Hi. Is there a way to limit the “navbar search” to only search WooCommerce? More specifically, I want to use native WC search engine. The reason for me being that some WC products have a “hidden” status. These will not show up in WC search, but they still show up when using the x theme navbar search (global WP search function).

Hi @bilievesser,

It’s possible. Please setup a child theme first:

After that add this following code under functions.php file locates in your child theme:


function search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('post_type', array( 'product' ) );
    }
  }
}

add_action('pre_get_posts','search_filter');

For more information, please take a look at this: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Hope it helps :slight_smile:

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