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 created an event CPT and a meta box with a date. I saved the date as post_meta in the form dd.mm.yyyy.

Now I want to create a shortcode that queries all posts which's dates meet the condition (e.g.)

(01.05.2013 <= {event_date} <= 21.08.2013)

I could convert the strings into Date objects or whatever. But I don't even have a slight approach how to query in comparison of custom meta_values.

Can you think of a good point to start off? Or would I need a complete different approach?

EDIT

I suddenly found something (maybe I got the right google-ideas). What I read suggested to save the dates in yyyy-mm-dd form. So I came up with the query:

query_posts(array('meta_query' => array(array('meta_key' => 'ows_schedule_field-date', 'meta_value' => '2013-08-15', 'type' => 'date', 'compare' => '=='))));

I got one post with exact this date. But unfortunately the query returns some random other post which didn't even has this meta.

EDIT 2

I also tried this one without success...

query_posts(array('post_type' => 'schedule_event', 'meta_key' => 'ows_schedule_field-date', 'meta_query' => array(array('key' => 'ows_schedule_field-date', 'value' => '2013-08-15', 'type' => 'date', 'compare' => '>='))));

When I use get_post_meta(285, 'ows_schedule_event-date', true); (285 is one of the posts) I get 2013-08-15.

share|improve this question

1 Answer 1

First, you have to store your dates in a form that sorts properly mathematically. Unix timestamps will do that. The only human-like calendar format that does it is YYYY-MM-DD. You have your dates saved backwards. Assuming you store as YYYY-MM-DD, then this should work:

$args = array(
  'meta_query' => array(
     array(
        'meta_key' => 'ows_schedule_field-date', 
        'meta_value' => array($date_one,$date_two), // both dates YYYY-MM-DD
        'compare' => 'BETWEEN'
     )
   )
)
$posts = new WP_Query($args);
share|improve this answer
    
Unfortunately it doesn't work. When I use meta_key and meta_value, the query returns a single post that doesn't even have this meta! When using key and value (like in lots of posts on the net) it returns nothing. And also limiting the post_type results in emptiness –  Julian Aug 7 '13 at 9:43
    
I already converted to yyyy-mm-dd and working with this format now. –  Julian Aug 7 '13 at 15:55
    
I will look into it later. –  s_ha_dum Aug 7 '13 at 16:26

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.