wordpress - WP_Query for product search behaves strangely - not fetching products with search query parameter added -
first of all, apology if not right place put question. searched lot did not find suitable answer anywhere.
i having strange problem woocommerce product search. in store have following 2 products:
- syska x110 – 11000 mah power bank
- htc mobile phone
when use 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 above 2 results fine. if add search parameter, i.e.
$args = array( 'post_type' => array('product'), 's' => 'htc' );
there no result when expect htc mobile phone come up.
there problem well. if change $args
array this:
$args = array( 'post_type' => array('product'), 's' => 'h' );
it brings records other post types have , omitting products. have 3 different custom post_types in application (trips, travelog , hotel-info). strange thing don't understand when have defined specific post type ('post_type' => array('product')
) why wordpress searching other post types?
to exact search being performed on other post types , product post type being ignored!!
removing 's' => 'h'
again starts producing products , no other post_type searched.
i screwed up! started working woocommerce 2 days ago. please help!
update
after doing more research have found strange!
with 's' => 'htc'
i.e.
$args = array( 'post_type' => 'product', 's' => 'htc' );
the original sql output is:
select sql_calc_found_rows wp_posts.id wp_posts 1=1 , (((wp_posts.post_title '%htc%') or (wp_posts.post_excerpt '%htc%') or (wp_posts.post_content '%htc%'))) , wp_posts.post_type in ('post', 'travelog', 'hotel-info', 'trips', 'package_tour') , (wp_posts.post_status = 'publish' or wp_posts.post_status = 'closed' or wp_posts.post_author = 1 , wp_posts.post_status = 'private' or wp_posts.post_author = 1 , wp_posts.post_status = 'hidden') order wp_posts.post_title '%htc%' desc, wp_posts.post_date desc limit 0, 10
which unlikely, because specified 'product' other post types other product being considered!
whereas,
$args = array( 'post_type' => 'product', );
outputs:
select sql_calc_found_rows wp_posts.id wp_posts 1=1 , wp_posts.post_type = 'product' , (wp_posts.post_status = 'publish' or wp_posts.post_status = 'closed' or wp_posts.post_statusi = 'private' or wp_posts.post_status = 'hidden') order wp_posts.post_date desc limit 0, 10
and tried this:
$args = array( 'post_type' => array('product', 'trip'), ); $q = new wp_query($args); echo $q->request;
which outputs:
select sql_calc_found_rows wp_posts.id wp_posts 1=1 , wp_posts.post_type in ('product', 'trip') , (wp_posts.post_status = 'publish' or wp_posts.post_status = 'closed' or wp_posts.post_author = 1 , wp_posts.post_status = 'private' or wp_posts.post_author = 1 , wp_posts.post_status = 'hidden') order wp_posts.post_date desc limit 0, 10
so, above little knowledge can tell me reason (i don't know is!) wordpress ignoring product post_type 's' => 'str'
parameter on!! why so? please tell me why wordpress this?
how can include products in search result using wp_query
?
Comments
Post a Comment