How to search for user last name in the WordPress Users dashboard

May 3rd, 2011  |  Published in Mind

Have a client with almost 2500 users in their WordPress site and they wanted to be able to search their users by last name. By default this is not enabled in WordPress core, so here is a quick function/hook you can add to your functions.php file to enable this:

function custom_user_list_search( $query ) {

	if ( is_admin() ) {

		global $wpdb;

		if ( is_array( $qv = $query->query_vars ) && ( $s = trim( $qv['search'], '* \t\n\0\x0B' ) )
				&& ( $s = "%" . esc_sql( like_escape( $s ) ) . "%" ) ) {

			if ( !preg_match( '/' . $wpdb->usermeta . '/', $query->query_from ) )
				$query->query_from .= " INNER JOIN `" . $wpdb->usermeta . "` ON `" . $wpdb->users . "`.`ID` = `" . $wpdb->usermeta . "`.`user_id`";

			$query->query_where .= " OR ( `" . $wpdb->usermeta . "`.`meta_key` = 'last_name' AND `" . $wpdb->usermeta . "`.`meta_value` LIKE '" . $s . "' ) ";

		}

	}

	return $query;
}
add_filter( 'pre_user_query', 'custom_user_list_search', 15 );

I will probably expand this a little and create a WordPress plugin for it (if I can find the time).

NOTE: The trim regex should be this – ‘* \t\n\r\0\x0B’ – but the plugin I’m using is stripping the \r

Tags: , , ,

Notable Tech Posts – 2009.08.30

August 30th, 2009  |  Published in Mind

http://net.tutsplus.com/articles/web-roundups/top-10-most-usable-content-management-systems/

http://webdesignledger.com/tutorials/13-super-useful-jquery-content-slider-scripts-and-tutorials

http://sixrevisions.com/tools/10-excellent-feedback-tools-for-web-designers/

http://www.cre8ivecommando.com/exclude-a-category-from-your-wordpress-index-page-830/

http://www.webdesignerdepot.com/2009/08/free-photoshop-brushes-sparkling-light-effects/

http://sixrevisions.com/javascript/build-an-elastic-textarea-with-ext-js/

http://adambrown.info/p/wp_hooks – WordPress Hooks Database

http://www.newmediacampaigns.com/page/create-a-jquery-calendar-with-ajax-php-and-a-remote-data-source

http://www.webdesignerdepot.com/2009/08/20-quick-tips-for-aspiring-freelancers/

http://www.sitepoint.com/blogs/2009/08/27/rate-your-stuff-with-coldfusion/

http://www.sitepoint.com/blogs/2009/08/27/questions-to-ask-before-taking-on-rush-projects/

http://net.tutsplus.com/tutorials/php/supercharge-your-css-with-php-under-the-hood/

http://www.jankoatwarpspeed.com/post/2009/07/28/reinventing-drop-down-with-css-jquery.aspx

Tags: , , , , , , , , , , ,