The version of the code here in a couple answers that modifies the meta_query parameter of the search's WP_Query in pre_get_posts was no longer searching the post_title. Adding the ability to search either the post title, OR meta values can't be done directly in WP_Query without modifying SQL unfortunately, as this question elaborates upon: Using meta query ('meta_query') with a search query ('s')
I've combined some of the techniques here to get a working version that avoids preg_replaces and too much SQL modification (I wish it could be avoided entirely). The only downside being that after a search, subtitle text at the top of the page says "Search results for ''". I've just hidden that with CSS for my plugin's custom post type.
/**
* Extend custom post type search to also search meta fields
* @param WP_Query $query
*/
function extend_cpt_admin_search( $query ) {
// Make sure we're in the admin area and that this is our custom post type
if ( !is_admin() || $query->query['post_type'] != 'your_custom_post_type' ){
return;
}
// Put all the meta fields you want to search for here
$custom_fields = array(
"your_custom_meta_field",
"your_custom_meta_field2",
"your_custom_meta_field3"
);
// The string submitted via the search form
$searchterm = $query->query_vars['s'];
// Set to empty, otherwise no results will be returned.
// The one downside is that the displayed search text is empty at the top of the page.
$query->query_vars['s'] = '';
if ($searchterm != ""){
// Add additional meta_query parameter to the WP_Query object.
// Reference: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
$meta_query = array();
foreach($custom_fields as $cf) {
array_push($meta_query, array(
'key' => $cf,
'value' => $searchterm,
'compare' => 'LIKE'
));
}
// Use an 'OR' comparison for each additional custom meta field.
if (count($meta_query) > 1){
$meta_query['relation'] = 'OR';
}
// Set the meta_query parameter
$query->set('meta_query', $meta_query);
// To allow the search to also return "OR" results on the post_title
$query->set('_meta_or_title', $searchterm);
}
}
add_action('pre_get_posts', 'extend_cpt_admin_search');
/**
* WP_Query parameter _meta_or_title to allow searching post_title when also
* checking searching custom meta values
* https://wordpress.stackexchange.com/questions/78649/using-meta-query-meta-query-with-a-search-query-s
* https://wordpress.stackexchange.com/a/178492
* This looks a little scary, but basically it's modifying the WHERE clause in the
* SQL to say "[like the post_title] OR [the existing WHERE clause]"
* @param WP_Query $q
*/
function meta_or_title_search( $q ){
if( $title = $q->get( '_meta_or_title' ) ){
add_filter( 'get_meta_sql', function( $sql ) use ( $title ){
global $wpdb;
// Only run once:
static $nr = 0;
if( 0 != $nr++ ) return $sql;
// Modified WHERE
$sql['where'] = sprintf(
" AND ( (%s) OR (%s) ) ",
$wpdb->prepare( "{$wpdb->posts}.post_title LIKE '%%%s%%'", $title),
mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
);
return $sql;
});
}
}
add_action('pre_get_posts', 'meta_or_title_search');