Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have been perfecting this bit of code below and ran into an issue. I was wondering how I can change the following code so that it will so the anniversary of any user in the next 30 days, not just by month (i.e, an anniversary on Jan. 15 would appear on my list on Dec. 15, not just once it was January)

Any suggestions (I'm running into such trouble because if I change the date sorting, I am not able to collect the correct user anniversary dates)

 $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 );    
 endforeach;  

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 ) )";     
endif;    
return $query; 
 }  
add_action( 'pre_user_query', 'anniversary_user_query' ); 
share|improve this question

1 Answer 1

I'm kinda confused on why you're passing 0 as meta_value, but if your storing unix timestamps under anniversary_date, this approach should be sufficient:

// queries anniversary dates between today and the next 30 days
    $query = new WP_Query(array(
        'order' => 'ASC',
        'orderby' => 'meta_value',
        'meta_key' => 'anniversary_date',
        'meta_value' => array(strtotime('today'), strtotime('+30 days')),
        'meta_compare' => 'BETWEEN'
    ));
share|improve this answer
    
Thanks, i plugged that in in place of $thismonth = date('m'); $query->query_where = "WHERE 1=1 AND ( $wpdb->usermeta.meta_key = 'anniversary_date' AND ( MONTH( $wpdb->usermeta.meta_value ) = $thismonth ) )"; endif; but it doesn't seem to be working, any suggestions? Thanks so much! –  pelle May 24 '12 at 19:02

Your Answer

 
discard

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.