do_action_ref_array( 'pre_user_query', WP_User_Query $this )

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


Description Description

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


Parameters Parameters

$this

(WP_User_Query) The current WP_User_Query instance, passed by reference.


Top ↑

Source Source

File: wp-includes/class-wp-user-query.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
3.1.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by tikot

    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.