WP_USERMETA表的联接查询

时间:2021-01-03 作者:Mukhyyar

我想检查用户时区是否为珀斯,是否属于A组。

我的查询不起作用

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\'
如果我在01和上使用,则此查询有效

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\'
enter image description here我需要检查的表

1 个回复
SO网友:Mukhyyar

    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;
}
通过以下方式实现所需结果WP_User_Query 感谢Tony Djukic和Tom J Nowell

相关推荐

除META_QUERY外,还使用WP_QUERY时有‘COMPARE’选项吗

我了解compare 在以下情况下:meta-query, 但我想知道是否有可能compare 在中的另一列上wp_posts 表viaWP_Query. 我先写了这个,效果很好: $results = $wpdb->get_results( $wpdb->prepare( \'SELECT id, post_title, post_name FROM \'.$wpdb->prefix