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

将搜索关键字添加到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 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上