kaiser's answer got me on the right track but I had to join the postmeta
table and add a group by as well.
Now like_escape
is $wpdb->esc_like
. Replace [your_post_type]
with your post type and [your_postmeta_key]
with your meta key:
//join postmeta for search
add_filter( 'posts_join' , function($join){
global $wpdb;
if(is_search() && is_admin() && $_GET['post_type'] == '[your_post_type]')
{
$join .= " LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
}
return $join;
});
//search [your_postmeta_key] for search string
add_filter( 'posts_where', function( $where )
{
global $wpdb;
if(is_search() && is_admin() && $_GET['post_type'] == '[your_post_type]')
{
$searchstring = '%' . $wpdb->esc_like( $_GET['s'] ) . '%';
//search [your_postmeta_key] as well
$where .= $wpdb->prepare(" OR ($wpdb->postmeta.meta_key = '[your_postmeta_key]' AND $wpdb->postmeta.meta_value LIKE %s) ", $searchstring);
}
return $where;
});
//group by post ID
add_filter( 'posts_groupby', function ($groupby, $query) {
global $wpdb;
if(is_search() && is_admin() && $_GET['post_type'] == '[your_post_type]')
{
$groupby = "{$wpdb->posts}.ID";
}
return $groupby;
}, 10, 2 );