Search Posts with Custom Fields as query

There are few ways to solve this problem…

1. Don’t bother and use Relevansi (or some other similar plugin)

You can often hear that using many plugins is a bad thing. It’s not entirely true. It’s bad only if these plugins have big impact on use of resources. In this case, Relevansi won’t cause such impact.

Of course there is another problem with plugins – you have to maintain them (updates, and so on).

2. Use posts_join and posts_where hooks

It’s the easiest way of solving your problem. Just join the posts and postmeta tables and add WHERE clauses that will search your value in proper custom fields.

// Join with postmeta table
function mycf_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .= " LEFT JOIN {$wpdb->postmeta} mycfpm ON ({$wpdb->posts}.ID = mycfpm.post_id ";
    }

    return $join;
}
add_filter('posts_join', 'mycf_search_join' );

// Add where clauses
function mycf_search_where( $where ) {
    global $wpdb;

    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "({$wpdb->posts}.post_title LIKE $1) OR (mycfpm.meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'mycf_search_where' );

// Prevent duplicates
function cf_search_distinct( $distinct ) {
    global $wpdb;

    if ( is_search() ) {
        return 'DISTINCT';
    }

    return $distinct;
}
add_filter( 'posts_distinct', 'mycf_search_distinct' );

The only problem with this solution is that it can be a little bit slow. Especially if you don’t want to search in all custom fields.

3. Custom CF indexing

You can also use save_post hook to create something like an index. After saving post you get all custom fields that should be searchable, combine their values and save as another custom field.

So let’s say there is a post with CFs: color=red, location=europe, name=john and all of them should be searchable. You can get these values, separate them with some separator char/sequence (for example #, so you’ll get red#europe#john) and save this string as new CF (let’s call it _cf_search_string).

function mycf_prepare_search_index_cf( $post_id ) {
    if ( wp_is_post_revision( $post_id ) ) return;

    $meta_keys = array('cf_key1', 'cf_key2', 'cf_key3');
    $search_string = '';
    foreach ( $meta_keys as $k ) {
        $search_string .= get_post_meta( $post_id, $k, true ) . '#';
    }
    update_post_meta( $post_id, '_cf_search_string', $search_string );
}
add_action( 'save_post', 'mycf_prepare_search_index_cf', 100, 1 );

Then you can use code from 2. to search only in this one custom field and not all of them, so it will add only one join and won’t multiply rows that will be searched during DB query.