Woocommerce Search Result showing post result not product result
I am using by default product search of woocommerce but search result is showing result like post NOT like product. my search get query is like
mydomainname/?s=men&post_type=product
means my theme only taking post so what can do for getting product type result.
Any help.
Thanks
<div class="advance-product-search">
<form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<?php
$terms = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent' => 0,
) );
?>
<?php if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) : ?>
<div class="advance-search-wrap">
<?php $current = ( isset( $_GET['product_category'] ) ) ? absint( $_GET['product_category'] ) : ''; ?>
<select class="select_products" name="product_category">
<option value=""><?php esc_html_e( 'All Categories', 'wen-associate' ); ?></option>
<?php foreach ( $terms as $cat ) : ?>
<option value="<?php echo esc_attr( $cat->term_id ); ?>" <?php selected( $current, $cat->term_id ); ?> ><?php echo esc_html( $cat->name ); ?></option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>
<div class="advance-search-form">
<input type="search" id="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>" class="search-field" placeholder="<?php echo esc_attr__( 'Search products…', 'wen-associate' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />
<input type="submit" value="" />
<input type="hidden" name="post_type" value="product" />
</div><!-- .advance-search-form -->
</form><!-- .woocommerce-product-search -->
Solutions
You can add this function in Your function.php
add_action( 'pre_get_posts', 'search_woocommerce_only' );
function search_woocommerce_only( $query ) {
if( ! is_admin() && is_search() && $query->is_main_query() ) {
$query->set( 'post_type', 'product' );
}
}
Similar questions
Result is not showing when I search a category with "&" keyword
When I search a category name with "&" then the search result is not showing in wordpress admin section. For example : when I search with key word "& test" then showing "No categories found." but when I search with only keyword "test" then it returns actual result. But in my category section there is a category name "test & test". Can y...
Advance Search not showing proper result in Wordpress?
I have added advance search in Wordpress Search.. please check below image.. Below is my search Code in wordpress : From above code i am getting proper result... but when i change the category the result stays same...?? If i get search results as 1,2,3,4,5,6,7,8,9 and on change on category i need 1,5,6,8,9 what is wrong with my code..??
Wordpress Search not Showing exact result for pages
I have a wordpress site with following Search function, it works fine with some pages but when i use exact same title on search it doesnt show some pages. What might be the reason for it? Please can anyone help me to sort out this issue. Big thanks in advance searchform.php code is like below
Why is my fav icon not showing in mobile search result?
Right now I've noticed that my icon is not showing in mobile search result. Only thing that is showing is pale blue earth icon thingy. It is perfectly fine on desktop tabs. I have also checked on https://www.google.com/s2/favicons?domain=website.com and the icon is there. It has been almost 3 months since this particular website is live, I tought a...
Garbage code showing up in search result post descriptions
I don't know why this is showing up on all my post descriptions in Google: It is driving me crazy. I am new to this but feel this is the reason my posts are not posting alone in Google. They are only tied to my home page in Google.
Custom Search Result Page displaying only 1 result.
I'm trying to build a custom search results page. The problem I'm running into is that when I enter a search term ; say 'gaming', it will only return one result, being the latest result. I have my blog setup to display only one article at a time. However, I found a way to circumvent the search results page by logging into wp-admin -> settings -> re...