WooCommerce Memberships first_name 검색 추가
WooCommerce Memberships에서 구독자 검색시 사용자 아이디, 이메일, 디스플레이 네임, 사용자 id값 에 해당하는 결과만 검색이 됩니다.
first_name 을 추가 검색 결과 값에 넣기 위해서는 아래 내용을 현재 활성화된 테마의 functions.php 파일에 추가가 필요합니다
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">add_filter( 'posts_clauses', 'search_wcmembership_custom', 90, 2 );
function search_wcmembership_custom( $pieces, \WP_Query $wp_query ) {
global $wpdb;
// bail out if not the correct post type
if ( 'wc_user_membership' !== $wp_query->query['post_type'] ) {
return $pieces;
}
// whether to add a join clause for users table or not
$join_users = false;
if ( isset( $wp_query->query['s'] ) ) {
$keyword = trim( ltrim( $wp_query->query['s'], '_' ) );
if ( ! empty( $keyword ) ) {
$join_users = true;
$keyword = '%' . $keyword . '%';
$first_name_key = "$wpdb->usermeta.meta_key = 'first_name'";
$first_name = $wpdb->prepare( "$wpdb->usermeta.meta_value LIKE %s", $keyword );
$where = $pieces['where'];
$where .= " OR ( ($first_name_key) AND ($first_name) )";
// replace the where clauses
$pieces['where'] = $where;
}
}
// join users table, if needed
if ( $join_users ) {
$pieces['join'] .= " INNER JOIN $wpdb->usermeta ON $wpdb->posts.post_author = $wpdb->usermeta.user_id AND meta_key = 'first_name'";
}
return $pieces;
}
</pre>