php - search users using user name and user meta in wordpress -
i saving wordpress user below:
$user_data = array( 'id' => '', 'user_pass' => '', 'user_login' => $user_name, 'user_email' => $trainer_email, 'first_name' => $first_name, 'last_name' => $last_name, 'role' => 'trainer' ); $random_password = wp_generate_password(8,false); $user_id = wp_insert_user( $user_data ); update_user_meta( $user_id, 'course_registered',$course_registered ); wp_set_password($random_password, $user_id);
presently using 'wp_user_query' method following arguments:
$args = array( 'role' => 'trainer', 'orderby' => 'display_name', 'posts_per_page' => '3', 'paged' => $paged, 'search' => '*'.$trainer_name.'*', 'search_columns' => array( 'user_login'), 'count_total' => true, 'number' => 5, 'meta_query' => array( array( 'key' => 'course_registered', 'value' => $course_name, 'compare' => 'like' ) ) ); $wp_user_query = new wp_user_query($args);
i want search user if either 'user name' or meta_key 'course_registered' else both. tried above& searched many didn't work one. please me..!
please try both way because not sure correct or not.
$args = array( 'role' => 'trainer', 'orderby' => 'display_name', 'posts_per_page' => '3', 'paged' => $paged, 'search' => '*'.$trainer_name.'*', 'search_columns' => array( 'user_login'), 'count_total' => true, 'number' => 5, 'meta_query' => array( 'relation' => 'or', array( 'key' => 'course_registered', 'value' => $course_name, 'compare' => 'like' ) ) ); $wp_user_query = new wp_user_query($args);
here add 'relation' => 'or' in meta query
or
use get_users method instead of wp_user_query
i have implement type of searching not sure in case please try both way 1 one.
Comments
Post a Comment