简体   繁体   中英

Get user id from wp_usermeta table

I have stored the certain term ids in wp_usermeta table. i want to get the user id where the term id match with the user meta key 'location' value. i have tried it with below query but it is doing nothing.

global $wpdb;
    $term = get_queried_object();
    $query = $wpdb->get_results("SELECT user_id FROM wp_usermeta WHERE meta_key='location' AND meta_value LIKE %s",'%'.$term->term_id.'%');

在此处输入图片说明

try this, you are not passing the value correctly

$query = $wpdb->get_results("SELECT user_id FROM wp_usermeta WHERE
meta_key='location' AND meta_value LIKE '%s,%".$term->term_id."%'");

Is there any reason for using a sql query? i think this will do what you want exactly :

$user_query = new WP_User_Query( array( 'meta_key' => 'location', 'meta_value' => 'ANY' ) );

foreach ($user_query as $user) {
    $user->ID; // the the user id
}

I have figured it out. Here is my solution. if someone needed.

$term = get_queried_object();
    $wpdb->get_results($wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key='location' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $term->term_id . '%'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM