douyingp82418 2018-03-26 19:39
浏览 70

在Woocommerce中扩展Admin产品搜索自定义元字段

I am trying to extend the ADMIN WooCommerce Product Search to include custom fields (e.g. _sku_2 which I added via functions)

Admin Product Search

Using this as a guide, I tried to add this code to my functions, but with no luck:

// 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 );
     };
}

It seems not to be editing the query results at all. Is the product search even using the default query?

  • 写回答

1条回答 默认 最新

  • dounan9070 2018-03-26 21:17
    关注

    This other way will work to extend admin product search for custom post meta keys values:

    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;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and work.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?