普通网友 2018-07-19 20:53
浏览 83
已采纳

将搜索关键字添加到Woocommerce产品查询中

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?

  • 写回答

1条回答 默认 最新

  • duanji1026 2018-07-25 13:28
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费