How to search in ACF's Custom Fields in Wordpress

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

He overlooks the Javascript e-learning and Wordpress projects in the company.

Add comment

Related blog articles