add_action('posts_search', function($search, $query){ global $wpdb; if ($query->is_mAIn_query() && !empty($query->query['s'])) { $sql = " OR EXISTS (SELECT * FROM {$wpdb->postmeta} WHERE post_id={$wpdb->posts}.ID and meta_key = 'product_no' and meta_value like %s)"; $like = '%' . $wpdb->esc_like($query->query['s']) . '%'; $search .= $wpdb->prepare($sql, $like); } return $search; },2,2);
只需要将上面的字段“product_no”替换成你所需的相关字段即可。