I had a hard time finding how to add custom field to admin search under woocommerce > members list. The woocommerce membership plugin is in fact using the ‘posts_clauses’ filter to filter the search result. So to add custom fields, here is the code I use :
function advanced_admin_custom_search($pieces, WP_Query $wp_query) {
global $wpdb;
if (!is_admin() ) return $pieces;
if (!array_key_exists('post_type', $wp_query->query)) return $pieces;
// bail out if not the correct post type
if ( 'wc_user_membership' === $wp_query->query['post_type'] ) {
// $wp_query->set( 'post_type', 'wc_user_membership' );
// search
if ( isset( $wp_query->query['s'] ) ) {
// add here your acf fields
$list_searcheable_acf = array("my_acf_field_name");
// get search expression
$terms = $wp_query->query_vars[ 's' ];
// explode search expression to get search terms
$exploded = explode( ' ', $terms );
if( $exploded === FALSE || count( $exploded ) == 0 )
$exploded = array( 0 => $terms );
// reset search in order to rebuilt it as we whish
$where = $pieces['where'];
foreach( $exploded as $tag ) :
$where .= "
OR EXISTS (
SELECT * FROM " . $wpdb->postmeta . "
WHERE post_id = " . $wpdb->posts . ".ID
AND " . $wpdb->posts . ".post_type = 'wc_user_membership'
AND (";
foreach ($list_searcheable_acf as $searcheable_acf) :
$where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value REGEXP '[[:<:]]{$tag}[[:>:]]') ";
endforeach;
$where .= ")
)";
endforeach;
// replace the where clauses
$pieces['where'] = $where;
}
}
return $pieces;
}
add_filter( 'posts_clauses', 'advanced_admin_custom_search', 500, 2 );