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
.
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: