凯撒的回答让我走上了正确的道路,但我必须加入postmeta
并添加分组依据。
现在like_escape
是$wpdb->esc_like
. 代替[your_post_type]
与您的帖子类型和[your_postmeta_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 );