0
votes

I'm developing shop in Woocommerce with my own theme. My problem is that when I want to search products in category by query website.com/?s=product&product_cat=category i get the result with all products from that category. My page: http://nsr.przedprojekt.com/wp/?s=kross&product_cat=rowery.

In functions.php I don't have declarated woocommerce and I don't want to do so. I've tried to override the archive-page.php template but this also didn't work.

1

1 Answers

1
votes

By default the wordpress search accept only one parameter: ?s=your-term

For add more functionality in a Wordpress search, you must create a new search configuration, that accept your extra parameter.

For Example.

If you want to search inside a specific category, you have to create a custom function like this:

// advanced search functionality
function advanced_search_query($query) {

    if($query->is_search()) {

        // your extra param is: in_category // &in_category=sample-category
        if (isset($_GET['in_category'])) {
            $query->set('category_name', $_GET['in_category']);
        }

        return $query;
    }

}
add_action('pre_get_posts', 'advanced_search_query', 1000);

By the way, this not ending here.

This a custom search inside normal Wordpress category, but this is the same logic for create a custom search for Woocommerce.

Simple you have to add more query variable in the wordpress WP_Query, like this:

// advanced search functionality
function advanced_search_query($query) {

    if($query->is_search()) {
        if (isset($_GET['in_category'])) {

            $query->set('tax_query', array(array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => array($_GET['in_category']) )
            ));
        }

        return $query;
    }

}
add_action('pre_get_posts', 'advanced_search_query', 1000);

Now, you can use a sintax like this: http://nsr.przedprojekt.com/wp/?s=kross&in_category=rowery