[英]Get user id from wp_usermeta table
I have stored the certain term ids in wp_usermeta table. 我已经将某些术语ID存储在wp_usermeta表中。 i want to get the user id where the term id match with the user meta key 'location' value.
我想获取用户ID,其中术语ID与用户元键“位置”值匹配。 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? 有什么理由使用sql查询吗? 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 . '%'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.