php 按名称和其他字段#wp #users搜索用户

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 按名称和其他字段#wp #users搜索用户相关的知识,希望对你有一定的参考价值。

/**
 Extend user search in wp-admin
 include first_name, last_name and other fields in the search results
 **/
add_action('pre_user_query','extend_user_search');
function extend_user_search( $user_query ){
	// make sure that this code will be applied only for user search
	if ( $user_query->query_vars['search'] ){
		$search_query = trim( $user_query->query_vars['search'], '*' );
		if ( $_REQUEST['s'] == $search_query ){
			global $wpdb;
			// let's search by users first name
			$user_query->query_from .= " JOIN {$wpdb->usermeta} fname ON fname.user_id = {$wpdb->users}.ID AND fname.meta_key = 'first_name'";
			$user_query->query_from .= " JOIN {$wpdb->usermeta} lname ON lname.user_id = {$wpdb->users}.ID AND lname.meta_key = 'last_name'";
			// you can add here any meta key you want to search by
			// $u_query->query_from .= " JOIN {$wpdb->usermeta} cstm ON cstm.user_id = {$wpdb->users}.ID AND cstm.meta_key = 'YOU CUSTOM meta_key'";
			// let's search by all the post titles, the user has been published
			//$u_query->query_from .= " JOIN {$wpdb->posts} psts ON psts.post_author = {$wpdb->users}.ID";
			// what fields to include in the search
			$search_by = array( 'user_login', 'user_email', 'fname.meta_value', 'lname.meta_value');
			// apply to the query
			$user_query->query_where = 'WHERE 1=1' . $user_query->get_search_sql( $search_query, $search_by, 'both' );
		}
	}
}

以上是关于php 按名称和其他字段#wp #users搜索用户的主要内容,如果未能解决你的问题,请参考以下文章

php WordPress插件,用于为WP多站点按站点存储自定义和内置用户字段。适合多语言设置。自定义为y

centos怎么编辑wp-config.php

按JSON字段搜索名称

WP 插入 post PHP 函数和自定义字段

WP is_user_logged_in() 在集成站点中不起作用

请告知mongoDB中全文搜索的最佳解决方案