How to search in ACF’s Custom Fields in WordPress

View in another language:
How to search in ACF's Custom Fields in Wordpress
Categories
Technologies
Author

Nat Outsourcify

Lead Web Developer
Date

As we already discussed in an article, at Outsourcify we favor the use of the ACF plugin to add custom fields to WordPress content. In brief it allows to add additional fields to WordPress posts, ACF offers a wide range of field types – text, textarea, rich texts, images, image galleries, links to other pages, etc – that allows to structure the data in a much better way than just sticking everything in the text editor. This has somehow been enhanced with the introduction of the Gutenberg editor in WordPress 5.0.0 in December 2018, but as we explained our choice is to combine the new editor with ACF.

Searching everywhere, including custom fields

ACF stands for Advanced Custom Fields and it actually only builds on top of the existing “custom fields” of WordPress, just adding a nicer UI and additional types of fields. Sadly, one thing that is missing from WordPress
out of the box is the possibility to search anywhere, including in custom fields.

Most online resources will offer to install a plugin, there are actually many of them that deal with the search feature, but as always in such a case, we always wonder if a plugin is really necessary and if what we want to achieve could not be done by adding a few lines of code to our theme.

A simple solution without a plugin

Basically the only thing needed is to add these few lines of code in the functions.php file of your theme :

// Make the search to index custom
/**
 * Extend WordPress search to include custom fields
 * http://adambalee.com
 *
 * Join posts and postmeta tables
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */
function cf_search_join( $join ) {
    global $wpdb;
    if ( is_search() ) {    
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    }
    return $join;
}
add_filter('posts_join', 'cf_search_join' );

/**
 * Modify the search query with posts_where
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */
function cf_search_where( $where ) {
    global $pagenow, $wpdb;
    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    }
    return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

/**
 * Prevent duplicates
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */
function cf_search_distinct( $where ) {
    global $wpdb;
    if ( is_search() ) {
        return "DISTINCT";
    }
    return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );

An even nicer way would be put this code in a separate file and require it from the functions.php file, which adds flexibility.

require_once( 'includes/functions/admin_search_custom_fields.php' );

In all of our themes, we include a number of small scripts like this one to handle specific tasks, enhance WordPress when needed on this particular project, we for example have scripts to :

  • Enhance the dashboard : it adds all custom posts to the “dashboard right now” widget, convenient as we nearly always use custom posts.
  • Customize the admin panel : it will replace the WordPress logos and links and add instead information about Outsourcify and links to our support and contact pages
  • Clean header : this one removes all the clutter that’s added by WordPress to the HTML header of pages
  • Rename posts : very simple but always useful, we mostly don’t want to call articles “posts”, something around the lines of “blog articles” or simple “articles” is much more obvious for our clients.
  • Remove admin bar : just to remove the admin bar on the website frontend, it very often stays in the way and damages the design.
  • Columns in the admin : another great way to avoid another plugin, just add additional columns to the admin panel list pages tables programmatically. Using a plugin like Admin Columns Pro is overkill (and not free) when you just want to add a thumbnail and custom taxonomy column to your tables.
Nat Outsourcify · Lead Web Developer

After graduating with a bachelor degree of Enterprise Software from the Suranaree University of Technology, Nat joined Outsourcify as a Front-End developer and E-learning specialist and with years of experience on such tasks he now manages the front-end team. He loves to discover new technologies and new ideas to improve our workflows.

Have a project in mind?
Let's start your project today

Contact Us
Have a project in mind?
Let's start your project today

Related blog articles

Technologies

Our Headless WordPress Journey with Astro.js and Vue.js

September 2, 2025

Our Headless WordPress Journey with Astro.js and Vue.js
Our Headless WordPress Journey with Astro.js and Vue.js
Resources

Building a B2B Product: Laying the Right Foundations from Day One

September 29, 2025

Building a B2B Product: Laying the Right Foundations from Day One
Building a B2B Product: Laying the Right Foundations from Day One
Technologies

A ResTech MVP in 1 Month

September 19, 2025

A ResTech MVP in 1 Month
A ResTech MVP in 1 Month
Resources

Why Taking Over a Development Project Is Always a Challenge

August 11, 2025

Why Taking Over a Development Project Is Always a Challenge
Why Taking Over a Development Project Is Always a Challenge
Technologies

From Vibe-Coded Prototype to Production-Ready: How Client Mockups Accelerate Our Work

August 5, 2025

From Vibe-Coded Prototype to Production-Ready: How Client Mockups Accelerate Our Work
From Vibe-Coded Prototype to Production-Ready: How Client Mockups Accelerate Our Work
Technologies

Outsourcify’s 2025 Tech Stack Driving Digital Excellence

August 4, 2025

Outsourcify’s 2025 Tech Stack Driving Digital Excellence
Outsourcify’s 2025 Tech Stack Driving Digital Excellence
Outsourcify Story

What Our Clients Say About Us: A Look at Outsourcify’s Google Reviews

July 30, 2025

What Our Clients Say About Us: A Look at Outsourcify’s Google Reviews
What Our Clients Say About Us: A Look at Outsourcify’s Google Reviews
Outsourcify Story

The Agency Developer: Beyond the Code

July 14, 2025

The Agency Developer: Beyond the Code
The Agency Developer: Beyond the Code
Resources

A Website Is Non-Negotiable in 2025 — But Its Content May Be Training AI

July 9, 2025

A Website Is Non-Negotiable in 2025 — But Its Content May Be Training AI
A Website Is Non-Negotiable in 2025 — But Its Content May Be Training AI
Resources

SaaS Tools Annual Cost Comparison for a 35-User Team – and What You Can Learn from Our Journey

June 11, 2025

SaaS Tools Annual Cost Comparison for a 35-User Team – and What You Can Learn from Our Journey
SaaS Tools Annual Cost Comparison for a 35-User Team – and What You Can Learn from Our Journey
Technologies

Recent Projects at Outsourcify: A Behind-the-Scenes Series

June 2, 2025

Recent Projects at Outsourcify: A Behind-the-Scenes Series
Recent Projects at Outsourcify: A Behind-the-Scenes Series
Resources

A Guide to Thailand’s Online Payment Gateways

May 4, 2025

A Guide to Thailand’s Online Payment Gateways
A Guide to Thailand’s Online Payment Gateways
Technologies

10 Programming Practices Worth Rethinking

April 29, 2025

10 Programming Practices Worth Rethinking
10 Programming Practices Worth Rethinking
Outsourcify Story

The Outsourcify Story #1: Lessons from a decade in Web Development

March 23, 2025

The Outsourcify Story #1: Lessons from a decade in Web Development
The Outsourcify Story #1: Lessons from a decade in Web Development
Technologies

Outsourcify partners with Sisense: the Power of Business Intelligence

February 16, 2025

Outsourcify partners with Sisense: the Power of Business Intelligence
Outsourcify partners with Sisense: the Power of Business Intelligence
Technologies

The 8 Archetypes of Software Engineers Every Team Needs (And How to Harness Their Superpowers)

February 6, 2025

The 8 Archetypes of Software Engineers Every Team Needs (And How to Harness Their Superpowers)
The 8 Archetypes of Software Engineers Every Team Needs (And How to Harness Their Superpowers)
Outsourcify Website

Eco-friendly and Accessible Websites: Building a Sustainable Digital Future

December 10, 2024

Eco-friendly and Accessible Websites: Building a Sustainable Digital Future
Eco-friendly and Accessible Websites: Building a Sustainable Digital Future
Technologies

The impact of API-centric approaches on software development

November 27, 2024

The impact of API-centric approaches on software development
The impact of API-centric approaches on software development
Technologies

How to know you can trust a web agency: A practical guide

November 15, 2024

How to know you can trust a web agency: A practical guide
How to know you can trust a web agency: A practical guide
Technologies

Who’s watching? A guide to privacy on websites and protecting your data

November 14, 2024

Who’s watching? A guide to privacy on websites and protecting your data
Who’s watching? A guide to privacy on websites and protecting your data
Technologies

Understanding the differences between MVP and MMP for smarter product development

November 13, 2024

Understanding the differences between MVP and MMP for smarter product development
Understanding the differences between MVP and MMP for smarter product development
Technologies

The top 3 strategic pitfalls that can derail a tech startup

November 8, 2024

The top 3 strategic pitfalls that can derail a tech startup
The top 3 strategic pitfalls that can derail a tech startup
Technologies

How to avoid AI project failures: lessons from automation

November 7, 2024

How to avoid AI project failures: lessons from automation
How to avoid AI project failures: lessons from automation
Technologies

The top 3 pitfalls facing CTOs and how to overcome them

October 31, 2024

The top 3 pitfalls facing CTOs and how to overcome them
The top 3 pitfalls facing CTOs and how to overcome them
Technologies

How do we extract the needs of a startup in the context of a Define Scope – Requirements Workshop?

October 29, 2024

How do we extract the needs of a startup in the context of a Define Scope – Requirements Workshop?
How do we extract the needs of a startup in the context of a Define Scope – Requirements Workshop?
Technologies

The vital role of a product owner in your web project

October 25, 2024

The vital role of a product owner in your web project
The vital role of a product owner in your web project
Technologies

How to choose a web agency: Top platforms to help you find a reliable partner

October 15, 2024

How to choose a web agency: Top platforms to help you find a reliable partner
How to choose a web agency: Top platforms to help you find a reliable partner
Technologies

The breadth of expertise required for Web Development

October 9, 2024

The breadth of expertise required for Web Development
The breadth of expertise required for Web Development
Technologies

Running daily, a day early: cron jobs for everyone

October 7, 2024

Running daily, a day early: cron jobs for everyone
Running daily, a day early: cron jobs for everyone
Company Activities

Behind the scenes: Triple baby party & reflecting on our company’s sociology

October 4, 2024

Behind the scenes: Triple baby party & reflecting on our company’s sociology
Behind the scenes: Triple baby party & reflecting on our company’s sociology
Technologies

Caching: Our number one suspect

August 9, 2024

Caching: Our number one suspect
Caching: Our number one suspect
Technologies

What is a database and how do you choose one for your web application project?

June 18, 2024

What is a database and how do you choose one for your web application project?
What is a database and how do you choose one for your web application project?
Technologies

Outsourcify’s expertise with the Astro framework

June 11, 2024

Outsourcify’s expertise with the Astro framework
Outsourcify’s expertise with the Astro framework
Technologies

What is an API and how does Outsourcify use them?

May 22, 2024

What is an API and how does Outsourcify use them?
What is an API and how does Outsourcify use them?
Technologies

Which LLMs are we using to facilitate the development at Outsourcify?

May 10, 2024

Which LLMs are we using to facilitate the development at Outsourcify?
Which LLMs are we using to facilitate the development at Outsourcify?
Technologies

Integrating an AI service for Real Estate

January 19, 2024

Integrating an AI service for Real Estate
Integrating an AI service for Real Estate
Technologies

User Interface(UI) design process, before & after.

January 14, 2021

User Interface(UI) design process, before & after.
User Interface(UI) design process, before & after.
Technologies

Why and how to migrate from Drupal to WordPress?

November 13, 2020

Why and how to migrate from Drupal to WordPress?
Why and how to migrate from Drupal to WordPress?
Resources

How to achieve 100 on PageSpeed Insights

May 3, 2019

How to achieve 100 on PageSpeed Insights
How to achieve 100 on PageSpeed Insights
Outsourcify Website

Why would you use WordPress for a corporate website in 2019?

March 17, 2019

Why would you use WordPress for a corporate website in 2019?
Why would you use WordPress for a corporate website in 2019?
Technologies

Creating your own blocks in Gutenberg with ACF

January 26, 2019

Creating your own blocks in Gutenberg with ACF
Creating your own blocks in Gutenberg with ACF
Technologies

How will the new version 5 and the Gutenberg editor impact your WordPress website?

December 5, 2018

How will the new version 5 and the Gutenberg editor impact your WordPress website?
How will the new version 5 and the Gutenberg editor impact your WordPress website?
Technologies

The most common reasons why WordPress sites are hacked

February 18, 2018

The most common reasons why WordPress sites are hacked
The most common reasons why WordPress sites are hacked
Technologies

Clear the mess of WordPress development with Timber and ACF

January 20, 2018

Clear the mess of WordPress development with Timber and ACF
Clear the mess of WordPress development with Timber and ACF