my website is a woocommerce site and it has blog posts and some pages. My problem is the search box. Everytime I searched it only shows the posts and pages results. It is not showing woocommerce products. How can I fix this wordpress search result?

Here is my wordpress search.php



Replace this with your search form and products will show in results:

 <form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <label for="s">Search</label>
    <input type="text" class="field" name="s" id="s" placeholder="Search Something" />
    <input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" />
    <input type="hidden" name="post_type" id="post_type" value="product"  />
|improve this answer|||||

You have in your query WHERE p.post_type IN ('post', 'page'), that is why it shows only the posts and pages. Add post type product to query: WHERE p.post_type IN ('post', 'page', 'product')

|improve this answer|||||
  • Wow thanks! It works. Last question, it shows big images on search. How can I edit that? – Dale Aug 1 '18 at 6:53
  • Try replace wp_get_attachment_url with wp_get_attachment_image_src in line 43. You will get array with keys: url, width, height, is_intermediate, then $url_img = $result['url']. Use for example thumbnail as size parameter in wp_get_attachment_image_src function. – nmr Aug 1 '18 at 7:01
  • Hi, I've added this $url_img = wp_get_attachment_image_src ($attachment_id, $size = '', $icon = false ); $url_img = $result['url']; but it is not showing any image. Just text. – Dale Aug 1 '18 at 7:26
  • @Dale $result = wp_get_attachment_image_src ( get_post_thumbnail_id($row->id), 'thumbnail'); $url_img = $result['url']; – nmr Aug 1 '18 at 7:30
  • still not showing thumbnail or image. – Dale Aug 1 '18 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.