普通网友 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 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法