Skip to content

Instantly share code, notes, and snippets.

@maor
Created September 19, 2012 17:46
Show Gist options
  • Save maor/3751054 to your computer and use it in GitHub Desktop.
Save maor/3751054 to your computer and use it in GitHub Desktop.
<?php
function maor_twitter_run_wp_user_search_query() {
/* There's a reason we're adding the action from here, you'll see later why. */
add_action( 'pre_user_query', 'maor_twitter_help_main' );
/* Create a new WP_User_Query object, limit recordset to 10 */
$wp_user_search = new WP_User_Query(
array(
'number' => 10
)
);
/* Make sure to remove this action since it may affect other user queries around the site. */
remove_action( 'pre_user_query', 'maor_twitter_help_main' );
/* Get them users! */
$users = $wp_user_search->get_results();
/* If $users is not null or an empty array, go on */
if ( ! empty( $users ) ) {
foreach ( $users as $user ) {
printf( "My display_name is %s. My ID number is %d.", $user->display_name, $user->ID );
}
}
}
add_action( 'init', 'maor_twitter_run_wp_user_search_query' );
function maor_twitter_help_main( $query ) {
global $wpdb;
/* The display_name value you're looking to match */
$display_name = 'Coen';
/* Search can either be for an exact match or a partial match */
if ( $use_like_syntax = true ) {
$query->query_where .= $wpdb->prepare( " AND $wpdb->users.display_name LIKE %s", '%' . like_escape( $display_name ) . '%' );
} else {
$query->query_where .= $wpdb->prepare( " AND $wpdb->users.display_name = %s", $display_name );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment