[Resolved] WordPress default search vs. Woocommerce Search

Home Forums Support [Resolved] WordPress default search vs. Woocommerce Search

Home Forums Support WordPress default search vs. Woocommerce Search

  • This topic has 6 replies, 3 voices, and was last updated 8 years ago by Tom.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #175695
    Lihas

    Hi Tom,

    I want to display in the WordPress deafult search only posts and I added this code into my function.php file.

    function SearchFilter($query) {
               if ($query->is_search) {
                      $query->set('post_type', 'post');
              }
             return $query;
     }
    add_filter('pre_get_posts','SearchFilter');

    However, adding the above code has an impact on my woocmmerce search form as the search form in Woocomerce does not display any results.

    Can you help me further?

    This is the search results of my WordPress default search

    http://www.kaufpiraten.de/?s=test

    and this of my Woocommerce search result

    http://www.kaufpiraten.de/?s=jeans&post_type=product

    Thanks.

    Lihas

    • This topic was modified 8 years, 1 month ago by Tom.
    #175721
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Give this a shot:

    function SearchFilter($query) {
        if ( $query->is_search ) {
            $query->set( 'post_type', 'post' );
        }
        if ( function_exists( 'is_woocommerce' ) ) : 
            if ( $query->is_search && is_woocommerce() ) {
                $query->set( 'post_type', 'product' );
            }
        endif;
        return $query;
     }
    add_filter('pre_get_posts','SearchFilter');
    #175868
    Lihas1987

    Hi Tom,

    this did not solved my issue.

    The following search is not working, altough I have woocommerce products labeled as jeans in my store.

    http://www.kaufpiraten.de/?s=jeans&post_type=product

    The default WordPress search is working.

    Lihas

    #175926
    Tom
    Lead Developer
    Lead Developer

    I just tested this without any functions at all and it worked: my-site.com/?s=ninja&post_type=product

    If you remove all of the functions above, does it not work?

    #176774
    Lihas1987

    Hi Tom,

    I solved it. I have to add the following condition “&& !is_woocommerce()” in the first if statement. This is the code I have used and it works.

    function SearchFilter($query) {
    if ( $query->is_search && !is_woocommerce()) {
    $query->set( ‘post_type’, ‘post’ );
    }
    if ( function_exists( ‘is_woocommerce’ ) ) :
    if ( $query->is_search && is_woocommerce() ) {
    $query->set( ‘post_type’, ‘product’ );
    }
    endif;
    return $query;
    }
    add_filter(‘pre_get_posts’,’SearchFilter’);

    Thanks a lot.

    Lihas

    #176795
    Lihas1987

    Hi Tom,

    this is the right code. Otherwise your search function in the admin area will not deliver any results.

    function SearchFilter($query) {
        if ( $query->is_search && !is_woocommerce() && !is_admin()) {
            $query->set( 'post_type', 'post' );
        }
        if ( function_exists( 'is_woocommerce' ) ) : 
            if ( $query->is_search && is_woocommerce() && !is_admin()) {
                $query->set( 'post_type', 'product' );
            }
        endif;
        return $query;
     }
    add_filter('pre_get_posts','SearchFilter');
    • This reply was modified 8 years ago by Tom.
    #176828
    Tom
    Lead Developer
    Lead Developer

    Awesome! Thanks for posting the solution 🙂

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.