Kelxya
Kelxya

Reputation: 15

Adding a search keyword to a Woocommerce product query

I'm setting up custom Woocommerce search functionality relying on pre_get_posts and URL queries, on the Woocommerce product category pages. The taxonomy and meta value filters are working fine, but I'm having trouble adding a keyword search (that I want to search the product titles and descriptions). I have a text input in the search form that appends to the url as ?partdescription=FOO, and I'm trying to grab that variable and insert it into the main query.

This is what I have so far:

add_action('pre_get_posts','alter_query');

function alter_query($query) {
    global $wp_query;

    // checks if we're on a shop category page
    if ($query->is_main_query() && is_product_category()) {
        // Set up Filters from URL Query
        $urlQuery = $_GET;

        // Part Description
        $description = isset($urlQuery['partdescription']) ? $urlQuery['partdescription'] : NULL;

        // Only modify the query for the parts category
        if ($query->query_vars['product_cat'] == 'parts') {

            if (!empty($description)) $query->set('s', $description);

        }

    }

}

This approach worked fine for setting taxonomy and meta queries, but modifying 's' throws a whole bunch of errors like this:

[19-Jul-2018 20:47:59 UTC] WordPress database error Unknown column 'Array' in 'group statement' for query SELECT   wp_posts.* FROM wp_posts  WHERE 1=1  AND wp_posts.ID IN (129,131,132,150,151,152,256,257,258,259,260,263,271,272,273,275,321,371,427,428) AND wp_posts.post_type = 'nav_menu_item' AND ((wp_posts.post_status = 'publish')) GROUP BY Array ORDER BY wp_posts.menu_order ASC  made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/x/woocommerce.php'), x_get_view, X_View_Router::render, include('/themes/cvbs/framework/views/integrity/woocommerce.php'), get_header, locate_template, load_template, require_once('/themes/x/header.php'), x_get_view, X_View_Router::render, include('/themes/x/framework/legacy/cranium/headers/views/header/base.php'), x_get_view, X_View_Router::render, include('/themes/cvbs/framework/legacy/cranium/headers/views/integrity/wp-header.php'), x_get_view, X_View_Router::render, include('/themes/x/framework/legacy/cranium/headers/views/global/_navbar.php'), x_get_view, X_View_Router::render, include('/themes/x/framework/legacy/cranium/headers/views/global/_nav-primary.php'), ubermenu, wp_nav_menu, wp_get_nav_menu_items, get_posts, WP_Query->query, WP_Query->get_posts

It also makes my main navigation and widget menus disappear.

Any help would be appreciated!

EDIT July 24th:

Still trucking away on this, and I've found a few similar issues here and here. The solution on those posts was to use $query->is_main_query(), but I'm already using that check in my code. The only difference I could note between those issues and mine is that they were for setting meta_key, while I'm trying to set s. Is s something that can only be modified on a search page? Is there any way I can mimic its functionality on an archive page?

Upvotes: 0

Views: 3838

Answers (1)

Kelxya
Kelxya

Reputation: 15

Solved the problem, posting my solution here just in case it helps anyone. Because I was trying to alter the query for Woocommerce products, I was able to use a different hook instead of pre_get_posts:

do_action( 'woocommerce_product_query', $query, $this );

The rest of the code ended up being more-or-less the same, just using that new $query variable instead of global $wp_query. $query->is_main_query() is also no longer needed.

Reference for Modifying the WooCommerce Product Query

Upvotes: 0

Related Questions