[wp-trac] [WordPress Trac] #31383: Add WP_Tax_Query support to WP_User_Query

WordPress Trac noreply at wordpress.org
Tue Jan 2 10:46:45 UTC 2024


#31383: Add WP_Tax_Query support to WP_User_Query
-----------------------------+-----------------------------
 Reporter:  desrosj          |       Owner:  (none)
     Type:  feature request  |      Status:  new
 Priority:  normal           |   Milestone:  Future Release
Component:  Query            |     Version:  4.2
 Severity:  normal           |  Resolution:
 Keywords:                   |     Focuses:
-----------------------------+-----------------------------

Comment (by rafasashi):

 This is how I implement taxonomy for WP_User_Query but I wish it was
 simpler...

 First set tax_query via pre_get_users the same way you would usually do
 for Posts:

 {{{
 add_filter( 'pre_get_users', function($query){

         $query->set('tax_query',$tax_query);

         return $query;

 },9999999);
 }}}

 This will store the tax query in $query->query_vars

 Now you can modify the query via users_pre_query

 {{{
 add_filter( 'users_pre_query', function($results,$query){

         if( !empty($query->query_vars['tax_query']) ){

                 global $wpdb;

                 $qv =& $query->query_vars;

                 if ( is_array( $qv['fields'] ) && count( $qv['fields'] ) >
 3 ) {

                         $qv['cache_results'] = false;
                 }

                 $tax_query_sql =
 get_tax_sql($query->query_vars['tax_query'], $wpdb->users, 'ID');

                 $query->request = "
                         SELECT {$query->query_fields}
                         {$query->query_from}
                         {$tax_query_sql['join']}
                         {$query->query_where}
                         {$tax_query_sql['where']}
                         {$query->query_orderby}
                         {$query->query_limit}
                 ";

                 $cache_value   = false;
                 $cache_key     = $query->generate_cache_key( $qv,
 $query->request );
                 $cache_group   = 'user-queries';

                 if ( $qv['cache_results'] ) {

                         $cache_value = wp_cache_get( $cache_key,
 $cache_group );
                 }

                 if ( false !== $cache_value ) {

                         $query->results     = $cache_value['user_data'];
                         $query->total_users = $cache_value['total_users'];
                 }
                 else {

                         if( is_array( $qv['fields'] ) ) {

                                 $query->results = $wpdb->get_results(
 $query->request );
                         }
                         else{

                                 $query->results = $wpdb->get_col(
 $query->request );
                         }

                         if ( isset( $qv['count_total'] ) &&
 $qv['count_total'] ) {

                                 $found_users_query = apply_filters(
 'found_users_query', 'SELECT FOUND_ROWS()', $query );

                                 $query->total_users = (int)
 $wpdb->get_var( $found_users_query );
                         }

                         if ( $qv['cache_results'] ) {

                                 $cache_value = array(

                                         'user_data'   => $query->results,
                                         'total_users' =>
 $query->total_users,
                                 );

                                 wp_cache_add( $cache_key, $cache_value,
 $cache_group );
                         }
                 }

                 return $query->results;
         }

         return $results;

 },0,2);
 }}}

 What about adding get_tax_sql() somewhere bellow?

 https://github.com/WordPress/WordPress/blob/4a146890164f80e595e80477be9f10ab78ff77a4
 /wp-includes/class-wp-user-query.php#L630

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/31383#comment:17>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list