WordPress admin’s default search functionality only covers post titles and content, leaving custom field data unsearchable. This limitation can significantly impact content management efficiency, especially for sites heavily utilising custom fields.
The provided code snippet below extends WordPress’s native search capability to include custom field values stored in the postmeta table, enhancing the admin search functionality.
Code Snippet to add advanced custom field search to WordPress backend
// Function to extend WordPress search to include custom fields
function search_custom_fields($search, $wp_query) {
// Only run in admin, when there's a search query
if (!$wp_query->is_admin || !$search || !isset($wp_query->query['s']))
return $search;
// Access WordPress database
global $wpdb;
// Get the search term from the query
$search_term = $wp_query->query['s'];
$search = '';
// Build SQL to search post title and content
$search .= "($wpdb->posts.post_title LIKE '%$search_term%') OR ($wpdb->posts.post_content LIKE '%$search_term%')";
// Add SQL to also search postmeta table for matching custom field values
$search .= " OR EXISTS (
SELECT * FROM $wpdb->postmeta
WHERE post_id = $wpdb->posts.ID
AND meta_value LIKE '%$search_term%'
)";
// Wrap the search conditions in parentheses and add AND
if (!empty($search)) {
$search = " AND ({$search}) ";
}
return $search;
}
// Hook the function to WordPress search
add_filter('posts_search', 'search_custom_fields', 10, 2);
Implementation Steps:
- Add the code to functions.php:
- Navigate to Appearance > Theme Editor
- Locate and open functions.php (Ideally in a child theme otherwise, it will be removed when you update)
- Paste the code at the bottom of the file
- Save changes
What it does:
- Searches through post title and content
- Includes custom field values in search results
- Only affects admin panel searches
- Maintains existing search functionality
How It Works:
The code implements a filter on ‘posts_search’ that:
- Checks if the search is performed in admin
- Retrieves the search term
- Constructs SQL query to include postmeta table
- Returns modified search parameters
Use Cases:
- Finding products by SKU
- Searching through custom metadata
- Locating posts by additional attributes
- Searching through extensive custom field data like advanced customer fields
Performance Impact:
The search expansion adds minimal overhead as it
- Only runs in admin panel
- Executes only during active searches
- Uses existing database connections
This solution provides a practical way to enhance WordPress admin search capabilities without plugins, improving content management efficiency for sites using custom fields.
How to add custom field searching for WordPress site users
As a bonus I have also written a code snippet to allow your sites users to this snippet should work with any theme that uses the WordPress search function including page builders like Divi.
/**
* Extends WordPress search to include custom fields
* Add to functions.php
*/
// Hook into the search filter for both frontend and admin
add_filter('posts_search', 'custom_search_acf_fields', 10, 2);
function custom_search_acf_fields($search, $wp_query) {
// Exit if no search term or if not main query
if (!$search || !$wp_query->is_search || !$wp_query->is_main_query()) {
return $search;
}
global $wpdb;
// Get search term
$search_term = $wp_query->query_vars['s'];
// Prevent SQL injection
$search_term = esc_sql($search_term);
// Remove existing search clause
$search = '';
// Build custom search query for both post content and meta
$search = " AND (
{$wpdb->posts}.post_title LIKE '%{$search_term}%'
OR {$wpdb->posts}.post_content LIKE '%{$search_term}%'
OR {$wpdb->posts}.post_excerpt LIKE '%{$search_term}%'
OR EXISTS (
SELECT * FROM {$wpdb->postmeta}
WHERE post_id = {$wpdb->posts}.ID
AND meta_value LIKE '%{$search_term}%'
)
)";
return $search;
}
// Prevent duplicate results
add_filter('posts_distinct', 'custom_search_distinct');
function custom_search_distinct($where) {
global $wp_query;
if ($wp_query->is_search) {
return "DISTINCT";
}
return $where;
}
// Fix ordering for search results
add_filter('posts_orderby', 'custom_search_orderby', 10, 2);
function custom_search_orderby($orderby, $wp_query) {
global $wpdb;
if (!$wp_query->is_search || !$wp_query->is_main_query()) {
return $orderby;
}
// Order by post title matches first, then content matches
$search_term = esc_sql($wp_query->query_vars['s']);
$orderby = "
CASE
WHEN {$wpdb->posts}.post_title LIKE '%{$search_term}%' THEN 1
WHEN {$wpdb->posts}.post_content LIKE '%{$search_term}%' THEN 2
WHEN {$wpdb->posts}.post_excerpt LIKE '%{$search_term}%' THEN 3
ELSE 4
END ASC,
{$wpdb->posts}.post_title ASC";
return $orderby;
}
The search functionality for ACF custom fields opens up powerful ways to help users find exactly what they’re looking for on your WordPress site. While this solution covers basic text searches, you can extend it further to include other field types like dates, numbers, addresses, or even relationship fields.
If you’ve implemented this search functionality on your site, share your experience in the comments below. Let us know if you’ve made any modifications or improvements to handle specific use cases on your sites.
And if you’re looking for search solutions for other ACF field types, drop a comment describing your needs – we’re always eager to explore new possibilities for enhancing WordPress search capabilities.
0 Comments