最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - Join Query on WP_USERMETA Table

programmeradmin0浏览0评论

I want to check if user time zone is perth and its belong from Group A.

MY QUERY IS NOT WORKING

SELECT * FROM `wp_users` AS t1 LEFT JOIN wp_usermeta AS t2 ON 
t1.ID=t2.user_id 
WHERE t2.meta_key='Time_Zone'
AND t2.meta_value = 'PERTH'
AND t2.meta_key='user_class_group'
AND t2.meta_value='Group A'

THIS QUERY IS WORKING IF I USED ON 01 AND

SELECT * FROM `wp_users` AS t1 LEFT JOIN wp_usermeta AS t2 ON 
t1.ID=t2.user_id 
WHERE (t2.meta_key='Time_Zone') 
AND t2.meta_value='PERTH'

TABLE WHICH I NEED TO CHECK

I want to check if user time zone is perth and its belong from Group A.

MY QUERY IS NOT WORKING

SELECT * FROM `wp_users` AS t1 LEFT JOIN wp_usermeta AS t2 ON 
t1.ID=t2.user_id 
WHERE t2.meta_key='Time_Zone'
AND t2.meta_value = 'PERTH'
AND t2.meta_key='user_class_group'
AND t2.meta_value='Group A'

THIS QUERY IS WORKING IF I USED ON 01 AND

SELECT * FROM `wp_users` AS t1 LEFT JOIN wp_usermeta AS t2 ON 
t1.ID=t2.user_id 
WHERE (t2.meta_key='Time_Zone') 
AND t2.meta_value='PERTH'

TABLE WHICH I NEED TO CHECK

Share Improve this question edited Jan 3, 2021 at 13:28 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jan 3, 2021 at 13:25 MukhyyarMukhyyar 1 5
  • 1 Have you tried using WP_User_Query instead? It's also possible to have taxonomies for users – Tom J Nowell Commented Jan 3, 2021 at 13:52
  • no actually I've almost finish this through query just stuck on one step on query. can you please help me out to get achieve result through query – Mukhyyar Commented Jan 3, 2021 at 13:58
  • I'm sorry I cannot, but there's a very easy way to get this data in PHP code, you do not need to make a raw SQL query – Tom J Nowell Commented Jan 3, 2021 at 14:15
  • can you guide me with WP_user_query? – Mukhyyar Commented Jan 3, 2021 at 14:25
  • You can start here: developer.wordpress/reference/classes/wp_user_query Try writing the query and then edit your question and someone can help you through the finer points. – Tony Djukic Commented Jan 3, 2021 at 18:14
Add a comment  | 

1 Answer 1

Reset to default 0
    function get_users_email ($time_zone_cont, $class_group){
        // Add Lisa's user id,  14, in an array.
        $exclude_list = array(  );
        $Time_Zone = $time_zone_cont;
        $user_class_group = $class_group;
        $args = array(
            // 'role' => 'Editor',
            // 'exclude' => $exclude_list,
             'meta_query' => array(
                // 'relation' => 'OR',
                array(
                    'key'     => 'Time_Zone',
                    'value'   => $Time_Zone,
                    'compare' => '='
                ),
                array(
                    'key'     => 'user_class_group',
                    'value'   => $user_class_group,
                    'compare' => '='
                )
                )
        );
         
        // Custom query.
        $my_user_query = new WP_User_Query( $args );
         
        // Get query results.
        $results = $my_user_query->get_results();
         
        // Check for editors
        if ( ! empty( $results ) ) {
                foreach ( $results as $result ) {
                    // Get each editor's data.
                    $output = get_userdata( $result->ID );
                    // Show editor's name.
                 $get_output[] = $output->user_email;  
                }
                
                
        }

return $get_output;
}

Achieve the desired result through WP_User_Query class thanks @Tony Djukic and @ Tom J Nowell

发布评论

评论列表(0)

  1. 暂无评论