WP_Query for product search behaves strangely – not fetching products with search query parameter added

First of all, my apology if this is not the right place to put this question. I searched a lot but did not find a suitable answer anywhere.

I am having a strange problem with Woocommerce Product search. In my store I have following two products:

  • Syska X110 – 11000 mAh Power Bank
  • HTC Mobile Phone

When I use the following WP_Query

$args = array(
   'post_type' => array('product')
);

$q = new WP_Query($args);
if($q->have_posts()) {
   while($q->have_posts()) : the_post();
     echo the_title() . '<br />';
   endwhile;
}

I get above two results fine. But if I add a search parameter, i.e.

$args = array(
  'post_type' => array('product'),
  's' => 'htc'
);

There is no result when I expect HTC Mobile Phone to come up.

There is another problem as well. If I change $args array to this:

$args = array(
    'post_type' => array('product'),
    's' => 'h'
);

It brings up records from other post types I have and omitting all products. I have 3 different custom post_types in my application (trips, travelog and hotel-info). The strange thing which I don’t understand is when I have defined a specific post type ('post_type' => array('product')) why WordPress is searching other post types?

To be exact the search is being performed on other post types only and product post type is completely being ignored!!

Removing 's' => 'h' again starts producing products and no other post_type is searched.

I am completely screwed up! I only started working with WooCommerce two days ago. Please help!

Read more here: WP_Query for product search behaves strangely – not fetching products with search query parameter added

Leave a Reply

Your email address will not be published. Required fields are marked *