WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 have a site I run which shows the weddings and anniversaries of some friends.

I have this running to show the users (and their anniversaries) according to what month it is:

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

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

 foreach( $users as $user ):    
echo $user->display_name;  
 echo ' will be celebrating their anniversary on ';     
echo get_user_meta( $user->ID, 'anniversary_date', true );   

function anniversary_user_query( $query ){  

 if( $query->query_vars['meta_key'] == 'anniversary_date' && $query->query_vars['meta_value'] === 0 ):    
     global $wpdb;        
     $thismonth = date('m');      
     $query->query_where = "WHERE 1=1 AND ( $wpdb->usermeta.meta_key = 'anniversary_date' AND ( MONTH( $wpdb->usermeta.meta_value ) = $thismonth ) )";    
return $query;
add_action( 'pre_user_query', 'anniversary_user_query' ); 

The dates in my database are structured like YYYY-MM-DD.

How can I sort the dates that will appear on the page so that they are ordered from earliest in the month (by day) to latest?


share|improve this question

Modify the query_orderby clause to order by day of month. Add this to your anniversary_user_query function:

$query->query_orderby = "ORDER BY DAY( $wpdb->usermeta.meta_value ) ASC";
share|improve this answer
Awesome! It worked! Thanks. Another quick question if anyone could help me, how could I change this code to not just pick up on a month, but a 30 day period (so someone's anniversary on jan 12 would show up starting dec 12)? – pelle May 23 '12 at 18:09

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.