Woocommerce Admin产品搜索不起作用

时间:2019-04-27 05:47:11

标签: wordpress search woocommerce

我遇到了这样的问题:尽管我的搜索输入中有很多产品,但管理员中的woocommerce产品搜索无法正常工作,并且给出的结果为No product found

我也没有使用relevancy search插件,所以不会有任何问题。

我在wp-congig.php中将debug设置为true,并通过打印整个查询抛出与wp_meta相关的一个错误。

还尝试禁用主题,并尝试使用默认主题,也尝试逐个禁用每个插件,但仍然没有运气。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的事情,并将以下代码块添加到我的functions.php文件中,立即修复了该问题:

function m_request_query( $query_vars ) {
    global $typenow;
    global $wpdb;
    global $pagenow;

    if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
        $search_term  = esc_sql( sanitize_text_field( $_GET['s'] ) );
    // Split the search term by comma.
        $search_terms = explode( ',', $search_term );
    // If there are more terms make sure we also search for the whole thing, maybe it's not a list of terms.
        if ( count( $search_terms ) > 1 ) {
            $search_terms[] = $search_term;
        }
    // Cleanup the array manually to avoid issues with quote escaping.
        array_walk( $search_terms, 'trim' );
        array_walk( $search_terms, 'esc_sql' );
        $meta_key               = '_sku';
        $post_types             = array( 'product', 'product_variation' );
        $query                  = "SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id WHERE postmeta.meta_key = '{$meta_key}' AND postmeta.meta_value IN  ('" . implode( "','", $search_terms ) . "') AND posts.post_type IN ('" . implode( "','", $post_types ) . "') ORDER BY posts.post_parent ASC, posts.post_title ASC";
        $search_results         = $wpdb->get_results( $query );
        $product_ids            = wp_parse_id_list( array_merge( wp_list_pluck( $search_results, 'product_id' ), wp_list_pluck( $search_results, 'parent_id' ) ) );
        $query_vars['post__in'] = array_merge( $product_ids, $query_vars['post__in'] );
    }

    return $query_vars;
}
add_filter( 'request', 'm_request_query', 20 );

向摘要mircian大喊。