【问题标题】:Extend Admin product search for custom meta fields in Woocommerce在 Woocommerce 中为自定义元字段扩展管理员产品搜索
【发布时间】:2018-09-05 01:31:28
【问题描述】:

我正在尝试扩展 ADMIN WooCommerce 产品搜索以包含自定义字段(例如,我通过函数添加的 _sku_2)

Admin Product Search

使用this as a guide,我尝试将此代码添加到我的函数中,但没有成功:

// EXTEND ADMIN PRODUCT SEARCH
add_action( 'pre_get_posts', 'extend_admin_search' );
function extend_admin_search( $query ) {

 // Extend search for document post type
 $post_type = 'product';
 // Custom fields to search for
 $custom_fields = array(
        "_supplier_sku",
        "_sku_2"
     );

     if( ! is_admin() )
          return;

     if ( $query->query['post_type'] != $post_type )
    return;

     $search_term = $query->query_vars['s'];

     // Set to empty, otherwise it won't find anything
     //$query->query_vars['s'] = '';

     if ( $search_term != '' ) {
          $meta_query = array( 'relation' => 'OR' );

          foreach( $custom_fields as $custom_field ) {
                array_push( $meta_query, array(
                     'key' => $custom_field,
                     'value' => $search_term,
                     'compare' => 'LIKE'
                ));
          }

          $query->set( 'meta_query', $meta_query );
     };
}

似乎根本没有编辑查询结果。产品搜索是否甚至使用默认查询?

【问题讨论】:

    标签: php sql wordpress search woocommerce


    【解决方案1】:

    这种其他方式将有助于扩展管理员产品搜索自定义帖子元键值:

    add_filter( 'posts_search', 'extend_product_search', 20, 2 );
    function extend_product_search( $where, $query ) {
        global $pagenow, $wpdb;
    
        if ( 'edit.php' != $pagenow || ! is_search() || ! isset( $query->query_vars['s'] ) || 'product' != $query->query_vars['post_type'] ) {
            return $where;
        }
        // Here your post meta keys
        $meta_keys = array('_supplier_sku', '_sku_2');
        $meta_keys = implode("','", $meta_keys);
        // get the search value
        $term      = sanitize_text_field( $query->query_vars['s'] );
        // Light SQL Query to get the corresponding product IDs 
        $search_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->prefix}posts as p
            LEFT JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
            WHERE pm.meta_key IN ('$meta_keys') AND pm.meta_value LIKE '%$term%'" );
        // Cleaning
        $search_ids = array_filter( array_unique( array_map( 'absint', $search_ids ) ) );
        // Alter the WHERE clause in the WP_Query search
        if ( count( $search_ids ) > 0 ) {
            $where = str_replace( 'AND (((', "AND ( ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . ")) OR ((", $where );
        }
        return $where;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试和工作。

    【讨论】:

    • 嗯...那也不起作用,我也停用了所有其他插件,以防万一发生冲突。
    • @SpectrumMike 我已经对代码进行了一些更新……所以现在对于这 2 个自定义帖子元键,搜索更加灵活和开放……请再试一次……它确实有效。
    • 非常感谢您尝试帮助我。我的安装一定有问题,因为它仍然不适合我。我会看看我是否可以用另一种方式工作。顺便说一句,它在前端工作,而不是管理员。这是令人沮丧的部分。
    • @SpectrumMike 这很奇怪......请告诉我,因为这个答案对其他人真的很有用,因为在这方面没有太多的工作。
    • @LoicTheAztec 有趣的是,它实际上在前端的 3.4.1 上工作(在我删除产品后类型检查之后)但只返回简单或父产品.. 现在总比没有好!
    猜你喜欢
    • 2022-01-19
    • 2020-12-11
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2021-10-25
    相关资源
    最近更新 更多