首先验证产品属性(meta_key) 通过运行此查询
注意:根据wordpress数据库更改表前缀mine是默认wp_
SELECT * FROM wp_postmeta WHERE meta_key LIKE \'%code%\'; /* get attribute name example (_sku, _stock, _stock_status, height, width )*/
现在将其添加到函数中。php文件
function Add_custom_search( $query )
{
if( ! is_admin() && $query->is_main_query() )
{
if ( $query->is_search() )
{
$meta_query = $query->get( \'meta_query\' );
$meta_query[] = array(
\'key\' => \'code\', /* Product Attribute Meta key Here example (_sku, _stock, _stock_status, height, width ) */
\'value\' => $query->query[\'s\'],
\'compare\' => \'LIKE\'
);
$query->set( \'meta_query\', $meta_query );
}
}
}
add_action( \'woocommerce_product_query\' , \'Add_custom_search\' );