Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created August 14, 2012 21:37
Show Gist options
  • Save wpsmith/3353269 to your computer and use it in GitHub Desktop.
Save wpsmith/3353269 to your computer and use it in GitHub Desktop.
WP_User_Query
<?php
/**
* WordPress WP_User_Query Comprehensive Reference
* Compiled by wpsmith - wpsmith.net
*
* CODEX: http://codex.wordpress.org/Class_Reference/WP_User_Query
* Source: http://core.trac.wordpress.org/browser/tags/3.4.1/wp-includes/user.php
*/
$args = array(
//////Defaults
'blog_id' => //(int) - defaults to $GLOBALS['blog_id']
'role' => '', //(string) -
//Possible Values://
//'subscriber'
//'contributor'
//'author'
//'editor'
//'administrator'
//'custom-role'
//////Meta Query - Custom Field Parameters - Show posts associated with a certain custom field.
// Simple Meta Query
'meta_key' => '', //(string) - Custom field key.
'meta_value' => '', //(string|array) - Custom field value.
'meta_compare' => '', //(string) - Operator to test the 'meta_value'.
//Possible values://
//'=', '!=', '>', '>=', '<', '<=',
//'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'
//Default value is '='.
'meta_type' => '', //(string) - Type of meta
//Possible values://
//'NUMERIC',
//'BINARY',
//'CHAR',
//'DATE',
//'DATETIME',
//'DECIMAL',
//'SIGNED',
//'TIME',
//'UNSIGNED'.
// Complex Meta Query
'meta_query' => array(
array(
'key' => 'wpsand_capabilities',
'value' => '"author"',
'compare' => 'not like',
),
array(
'key' => '',
'value' => '',
'compare' => '',
),
),
//////Users Include/Exclude
'include' => array(), //(array) - Array of user IDs to include
'exclude' => array(), //(array) - Array of user IDs to exclude
//////Search Paremeters
'search' => '', //(string) - String value to search for
'search_columns' => array(), //(array) - Array of Table Columns to search
//Possible Values://
//array( 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename' )
//Possible Defaults://
//Defaults to user_email if '@' is present in 'search' arg
//Defaults to user_login & ID if 'search' arg is numeric
//Defaults to user_url if 'search' arg contains http:// or https://
//ELSE Defaults to user_login & user_nicename for strings
//////Order & Orderby Parameters - Sort retrieved posts.
'orderby' => 'user_login', //(string) - Sort retrieved users by parameter. Defaults to 'date'.
//Possible Values://
//'user_nicename' OR 'nicename'
//'user_email' OR 'email'
//'user_url' OR 'url'
//'user_registered' OR 'registered'
//'name' OR 'display_name'
//'post_count'
//'ID'
'order' => 'ASC', //(string) - Designates the ascending or descending order of the 'orderby' parameter. Defaultto 'ASC'.
//Possible Values://
//'ASC' - ascending order from lowest to highest values (1, 2, 3; a, b, c).
//'DESC' - descending order from highest to lowest values (3, 2, 1; c, b, a).
//////Offset & Limit Parameters - Useful in pagination
'offset' => '', //(int) - number of users to displace or pass over.
'number' => '', //(int) - number of users to return.
//////Count Parameters (SQL_CALC_FOUND_ROWS)
'count_total' => true, //(boolean) - true/false whether to count total.
//////SELECT Parameters
'fields' => 'all', //(string) - Desired fields to appear in the results
//Possible Values://
//'id' - WP_User_Query::get_results() array of IDs
//'all' - WP_User_Query::get_results() array of StdClass Objects (same as get_users)
//'all_with_meta' - WP_User_Query::get_results() array of WP_User Objects
//array( 'ID', 'user_login', 'user_pass', 'user_nicename', 'user_email', 'user_url', 'user_registered', 'user_activation_key', 'user_status', 'display_name' ), // Or, any selection of these
//////User level parameter
'who' => 'authors', //(string) - Retrieves authors, those who have user_levels > 0, Cannot be used with 'meta_key', or 'orderby' => 'meta_value' cf. http://core.trac.wordpress.org/ticket/21581
);
// Create the WP_User_Query object
$user_query = new WP_User_Query( $args );
// Get the results
$authors = $author_query->get_results();
// Output results
foreach( $authors as $author ):
// do something
endforeach;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment