I have created user meta that holds user birthday (birthday).

birthday is date field.

Now i am trying to figure how to sort (get) all users that have birthday based on specific month.

I have created drop menu that holds all the months.

function users_months_dropdown() {
<select name="month" id="month" onchange="" size="1">
    <option value="01">January</option>
    <option value="02">February</option>
    <option value="03">March</option>
    <option value="04">April</option>
    <option value="05">May</option>
    <option value="06">June</option>
    <option value="07">July</option>
    <option value="08">August</option>
    <option value="09">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
        submit_button( __( 'Filter Date' ), 'secondary', 'filter_date_action', false );
add_action( 'restrict_manage_users', 'users_months_dropdown' );

and now i stack with the query

i also try

function extended_user_searchs( $user_query ){
    if( isset( $_GET['month'] ) && !empty( $_GET['month'] ) ) {
        $date = $_GET['month'];
        $user_query->query_where .= " AND MONTH(birthday) = $date";
add_action( 'pre_user_query', 'extended_user_searchs' );
  • 1
    The ["custom fields" section]( codex.wordpress.org/Class_Reference/WP_User_Query) in the Codex doesn't help?
    – s_ha_dum
    Jul 4, 2015 at 15:02
  • Hi @s_ha_dum, How can you compare value only by month and not full date?
    – need-help
    Jul 4, 2015 at 15:10
  • By sort here you mean to filter? How do you store the birthday dates?
    – birgire
    Jul 4, 2015 at 15:46
  • Hi @birgire yes i mean filter to get new list in users.php based on filter. i store the date like dd/mm/yyyy
    – need-help
    Jul 4, 2015 at 15:48
  • Hi @Pieter Goosen i use datepicker and dateFormat : 'dd-mm-yy' this is for hebrew language
    – need-help
    Jul 4, 2015 at 16:29

1 Answer 1


User Meta Query with RLIKE:

If the birth date is stored like dd/mm/yyyy, in the user meta, then you could find all August birthday users with the following meta query:

'meta_query' => [
        'key'     => 'birthday',
        'value'   => '/08/',
        'compare' => 'RLIKE'

You could also consider storing the birthday month additionally in the user meta.

Here I've listed the available meta comparisons.

Applied to your code snippet:

Part #1

Here's how you can apply this to your code example:

 * Setup the user meta query for the current birth month
add_action( 'pre_get_users', function( $user_query ) 
    // User input:
    $birthmonth = filter_input( INPUT_GET, 'birthmonth', FILTER_SANITIZE_NUMBER_INT );

    // Setup the meta query:
    if( $birthmonth ) 
        $user_query->query_vars['meta_key']     = 'birthday';
        $user_query->query_vars['meta_value']   = zeroise( $birthmonth, 2 );
        $user_query->query_vars['meta_compare'] = 'RLIKE';
} );          

where we use the pre_get_users hook instead of the pre_user_query and use the handy zeroise() function - it's informative to check out the \WP_Locale class. We will use it's get_month() method in the second part here below.

Note that in your current attempt of directly modifying the SQL query, the birthday field is treated as a part of the wp_users table. Also the INNER JOIN, on the wp_usermeta table, is missing.

You must also validate/sanitize the user input.

Part #2

The second part is modified to:

 * Add a birth month filter to the users table
add_action( 'restrict_manage_users', function()
    global $wp_locale;

    // User input:
    $birthmonth = filter_input( INPUT_GET, 'birthmonth', FILTER_SANITIZE_NUMBER_INT );

    // Birthmonth select box: 
    print '<select name="birthmonth" id="birthmonth" onchange="" size="1">';
    printf( '<option value="">%s</option>', __( 'Birthmonth' ) );
    foreach( range(1,12) as $month )
            '<option value="%d" %s>%s</option>', 
            selected( $birthmonth, $month ), 
            $wp_locale->get_month( $month )  
    print '</select>';
} );

where we use birthmonth instead of month, to avoid name collision.

I also added the default state, without any birthmonth selected.

Also added the handy selected() function, to show the current selected option.

The month names are defined in the \WP_Locale class and the class instance is available through the global $wp_locale object.

  • thanks but seems i cant get this work. i also update my quasion and add the "query" i have tried.
    – need-help
    Jul 4, 2015 at 16:10
  • I updated the answer with a workable code example, dependend on the date format dd/mm/yyyy that you mentioned in a comment above @need-help
    – birgire
    Jul 4, 2015 at 21:53
  • Great answer! Thanks. by the way, i get this work some time after i have post this question. i make this work using pre_get_users and using $query->query_vars
    – need-help
    Jul 5, 2015 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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