do_action_ref_array( ‘pre_user_query’, WP_User_Query $query )

Fires after the WP_User_Query has been parsed, and before the query is executed.

Description

The passed WP_User_Query object contains SQL parts formed from parsing the given query.

Parameters

$queryWP_User_Query
Current instance of WP_User_Query (passed by reference).

Source

do_action_ref_array( 'pre_user_query', array( &$this ) );

Changelog

VersionDescription
3.1.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Find user by usermeta add change the meta key or add more meta keys OR meta_key='key_name' between () on WHERE line

    <?php
    
    add_action( 'pre_user_query', function( $uqi ) {
        global $wpdb;
    
        $search = '';
        if ( isset( $uqi->query_vars['search'] ) )
            $search = trim( $uqi->query_vars['search'] );
    
        if ( $search ) {
            $search = trim($search, '*');
            $the_search = '%'.$search.'%';
    
            $search_meta = $wpdb->prepare("
            ID IN ( SELECT user_id FROM {$wpdb->usermeta}
            WHERE ( ( meta_key='first_name' OR meta_key='last_name' )
                AND {$wpdb->usermeta}.meta_value LIKE '%s' )
            )", $the_search);
    
            $uqi->query_where = str_replace(
                'WHERE 1=1 AND (',
                "WHERE 1=1 AND (" . $search_meta . " OR ",
                $uqi->query_where );
        }
    });

You must log in before being able to contribute a note or feedback.