Saturday, October 8, 2022
HomeWordPress Developmentwp admin - Lengthen person search within the Wp backend space on...

wp admin – Lengthen person search within the Wp backend space on the customers.php web page to permit for looking out by electronic mail area and function from the “customers search” enter field


I’ve been seeking to lengthen the power to go looking customers within the backend for a while and have tried variations of a number of capabilities really helpful in these articles and posts together with others not linked to proven beneath –

-https://wordpress.stackexchange.com/questions/339209/making-users-php-search-include-a-specific-user-meta-data-field-without-messing

-https://rudrastyh.com/wordpress/pre_user_query.html

-https://wordpress.stackexchange.com/questions/5811/how-to-search-all-user-meta-from-users-php-in-the-admin

I’ve used the perform beneath as a template –

add_action('pre_user_query','rudr_extend_user_search');

perform rudr_extend_user_search( $u_query ){
// guarantee that this code might be utilized just for person search
if ( $u_query->query_vars[‘search’] ){
$search_query = trim( $u_query->query_vars[‘search’], ‘*’ );
if ( $_REQUEST[‘s’] == $search_query ){
international $wpdb;

        // let's search by customers first title
        $u_query->query_from .= " JOIN {$wpdb->usermeta} fname ON fname.user_id = {$wpdb->customers}.ID AND fname.meta_key = 'first_name'";
        
        // you possibly can add right here any meta key you need to search by
        // $u_query->query_from .= " JOIN {$wpdb->usermeta} cstm ON cstm.user_id = {$wpdb->customers}.ID AND cstm.meta_key = 'YOU CUSTOM meta_key'";

        // let's search by all of the put up titles, the person has been revealed
        $u_query->query_from .= " JOIN {$wpdb->posts} psts ON psts.post_author = {$wpdb->customers}.ID";
        
        // what fields to incorporate within the search
        $search_by = array( 'user_login', 'user_email', 'fname.meta_value', 'psts.post_title' );
        
        // apply to the question
        $u_query->query_where="WHERE 1=1" . $u_query->get_search_sql( $search_query, $search_by, 'each' );
    }
}

}

and in addition developed a perform which isn’t working in any respect the place I’m trying gather an electronic mail area and function if they’re entered into the search enter as a string akin to “@yahoo contributor” by imploding the string after which trying to find the primary worth within the customers desk utilizing a LIKE operator and carry out a JOIN to the usermeta desk the place I might then search wp_capabilities for a worth that’s corresponding to the function that might be the second worth of the imploded string. I exploit the “pre_user_query” hook with my perform however am solely getting errors and really feel I am very distant from an answer. Any assist or steering could be very appreciated.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments