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 am trying to perform a query on a custom-type post using the value of a custom field. This field contains a string date: dd.mm.yyyy. The post type name is 'event' The custom field name is 'date_event'

I would like to show all the EVENT that have the month of DATE_EVENT matching with the current month.

Any suggestion pls?

share|improve this question
1  
show what you have tried. the first step is to reverse the order of the dates you're storing to be in MySQL date format - yyyy-mm-dd –  Milo May 7 '13 at 21:09
    
you mean that the value must not be a string but a MySql date? –  Salvatore Dibenedetto May 7 '13 at 21:14
    
a string, but the units should be in descending order from large to small- year, month, day. if you think of the dates as just one big number, and then adding, subtracting, comparing them, it makes sense why it should be in that order. –  Milo May 7 '13 at 21:25
    
ok, done. but now how can i use this value to perform my query? –  Salvatore Dibenedetto May 7 '13 at 21:28
1  
Can you show your current code? –  vancoder May 7 '13 at 21:46
show 1 more comment

1 Answer

I did something similar, the technique you need to use is called a meta query.

Here is the query I wrote to get posts based on a date value stored as a custom field meta value.

query_posts(
array(
'post_type'=>'post',
'order'=>'ASC',
'orderby'=>'meta_value_num',
'meta_key'=>'date_event',
'posts_per_page'=> -1,
'meta_query' => array(
    'relation' => 'AND',
       array(
           'key' => 'date_event',
           'value' => $startDate, // Lowest date value
           'compare' => '>='
       ),
       array(
           'key' => 'date_event',
           'value' => $endDate, // Highest date value
           'compare' => '<='
       )
   )
));

'orderby' => 'meta_value_num' will sort the results by the meta value

'meta_key' => 'date_event' tells what the custom field is called

meta_query is an array which specifies that the results must match both conditions, then we specify a minimum date and a maximum date.

The date values must be stored either as YYYY-MM-DD or as a "Seconds since the Unix Epoch" value.

share|improve this answer
add comment

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.