Extending WordPress Search to Include Custom Fields
By default, WordPress search only looks at post titles and content. However, you often need to include custom field values—like product numbers, SKUs, or author info—in search results. This tutorial shows how to extend WordPress search to query specific custom fields using code.
Core Implementation Code
The following snippet uses the posts_search filter to modify the main query's SQL, adding a condition to match a custom field value.
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;
}, 10, 2);
Code Explanation
- Hook & Parameters: The
add_actionhooks into theposts_searchfilter, receiving the current search SQL clause ($search) and the WP_Query object ($query). We check$query->is_main_query()and the presence of a search term to ensure we only modify the front‑end main search query. - SQL Extension: The core logic builds a sub‑query that checks if a custom field record exists for the current post (matching
post_id) where the field name (meta_key) is'product_no'and the field value (meta_value) contains the search keyword (usingLIKE). - Security:
$wpdb->esc_like()escapes the search term, and$wpdb->prepare()safely prepares the SQL statement, preventing SQL injection.
Customization Example
Replace 'product_no' with your actual custom field name. To search multiple fields, adjust the SQL condition:
add_action('posts_search', function($search, $query) {
global $wpdb;
if ($query->is_main_query() && !empty($query->query['s'])) {
$like = '%' . $wpdb->esc_like($query->query['s']) . '%';
$sql = " OR EXISTS (SELECT * FROM {$wpdb->postmeta} WHERE post_id={$wpdb->posts}.ID AND meta_key IN ('sku', 'author_name') AND meta_value LIKE %s)";
$search .= $wpdb->prepare($sql, $like);
}
return $search;
}, 10, 2);
Best Practices & Considerations
- Performance:
LIKEqueries on thepostmetatable can be slow on large databases. Consider indexing relevant fields or using a dedicated search solution (e.g., Elasticsearch) for heavy‑duty needs. - Code Placement: Add this code to your child theme's
functions.phpor a functionality plugin. - Testing: Always test the search after implementation to verify it works as expected and doesn't break other queries.
Tip: This method modifies the core SQL query and is suitable for quickly adding custom‑field search. For more complex requirements (weighted search, excluding fields), consider a dedicated search plugin or a custom query class.