How to Enable Custom Field Search in WordPress Admin (no plugin)

Originally Published: 14 January 2025
Last updated: 14 January 2025
Written by: Nick Jolliffe

Reading Time: 4.105 minutes

Categories: Code Snippets
How to Enable Custom Field Search in WordPress Admin (no plugin)

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:

  1. Add the code to functions.php:
  2. Navigate to Appearance > Theme Editor
  3. Locate and open functions.php (Ideally in a child theme otherwise, it will be removed when you update)
  4. Paste the code at the bottom of the file
  5. 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:

  1. Checks if the search is performed in admin
  2. Retrieves the search term
  3. Constructs SQL query to include postmeta table
  4. 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.

Contact Us Today

Get Your Paws on Great Content Marketing and Communications

Join the discussion!

Let us know what you think about this post below!

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Related Blogs

No Results Found

The page you requested could not be found. Try refining your search, or use the navigation above to locate the post.

Roaring Content Logo

Looking for Great Content for your website?

We created Roaring Content as a fixed price content solution for your web site.

We can write your blogs, web copy, and more!

Our blog post subscription service delivers content to you each month, our fixed price plans all include a bespoke content strategy and there is a solution for any budget.

Click Here to find out more

Need help with your Marketing?

At Lion Spirit Media, we want to help you grow your business.

From creating great bespoke content, SEO and online advertising we are confident that we will increase your business and leads.

If you want to find out more, click the button below or send us an email.