WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I added a custom field to the user profile that asked for the date users joined my club. How can i pull this data and display on a page to celebrate anniversaries of users?

(for example: For may, this page would show all users who had originally joined in May by name and month and day)

I know I need a loop for this, just not sure how to pull in the data from the profile field.

share|improve this question

You can use the WP_User_Query class to query on user meta, however, your specific example of querying for a list of users with a join date matching a specific month and from any year requires a query more complex than what you can do out of the box with the WP_User_Query class.

First, it's important that the date format be correct and strictly enforced if you're going to be querying on this data. I'd suggest using a jQuery datepicker widget to enable input of dates into the join date field. YYYY-MM-DD is the standard MySQL date format.


Here's an example of a simple query that will load all users who joined this month:

$first = new DateTime();
$first->modify( 'first day of this month' );
$first = $first->format( 'Y-m-d' );

$args = array(
    'meta_key' => 'join_date',
    'meta_value' => $first,
    'meta_compare' => '>=',

$user_query = new WP_User_Query( $args );


Here's a more complex meta query that will load users between specific dates:

$first = new DateTime();
$first->modify( 'first day of this month' );
$first = $first->format( 'Y-m-d' );
$last = new DateTime();
$last->modify( 'last day of this month' );
$last = $last->format('Y-m-d');

$args = array(
    'meta_query' => array(
            'key' => 'join_date',
            'value' => array( $first, $last ),
            'compare' => 'BETWEEN',
            'type' => 'DATE'

$user_query = new WP_User_Query( $args );


For custom queries, you can modify the SQL directly. The first step is to set up a simple query:

$args = array(
    'meta_key' => 'join_date',
    'meta_value' => 0,

$user_query = new WP_User_Query( $args );
$users = $user_query->get_results();

foreach( $users as $user ):
    echo $user->user_nicename;
    echo ' joined ';
    echo get_user_meta( $user->ID, 'join_date', true );

Next, we add an action to pre_user_query to check for our simple query, and modify the SQL to just match the month and ignore year:

function wpse52318_user_query( $query ){
    if( $query->query_vars['meta_key'] == 'join_date' && $query->query_vars['meta_value'] === 0 ):
        global $wpdb;
        $thismonth = date('m');
        $query->query_where = "WHERE 1=1 AND ( $wpdb->usermeta.meta_key = 'join_date' AND ( MONTH( $wpdb->usermeta.meta_value ) = $thismonth ) )";
    return $query;
add_action( 'pre_user_query', 'wpse52318_user_query' );
share|improve this answer
Wow! Thanks for your help. Per your website, I had previously sent you a thank you telepathically, but you never know about how those things are received...so I wanted followed up with a post here. If I can ask a follow-up question, where do I need to put this in order to view the results on the page. Do I need to just create a custom template with this as the body? Thanks. – Oliver May 16 '12 at 18:04
@Oliver - the query would go in whatever template you wanted to display it in. the pre_user_query action and associated function should go in your theme's functions.php file. – Milo May 16 '12 at 19:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.