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 need to query posts by two meta fields and the wo_query I'm doing it with is returning nothing. Obviously, it's broken. Can somebody tell me what I'm doing wrong?

$args = array(
                    'post_type' => 'espresso_event',
                    'post_status' => 'publish',
                    'posts_per_page' => 8,
                    'category_name' => 'homepage',
                    'meta_query' => array(
                        array(
                            'key' => 'event_start_date',
                            'value' => date('Y-m-d'),
                            'compare' => '>=',
                            'type' => 'DATE'
                            ),
                        array(
                            'key' => 'event_end_time',
                            'value' => date('g:i A'),
                            'compare' => '>=',
                            'type' => 'TIME'
                            )
                        ),
                    'orderby' => 'meta_value',
                    'order' => 'ASC' // change this to ASC if you want newer events on top
                );
        // this saves the query to a temporary location so we can go back to it later after we run our query
        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query();
        $wp_query->query($args);

The query works without this part:

array(
                            'key' => 'event_end_time',
                            'value' => date('g:i A'),
                            'compare' => '>=',
                            'type' => 'TIME'
                            )

So obviously the problem is there but I don't get an error or anything so I can't see what the problem is.

share|improve this question
1  
can we see an example of the dates passed in? Why are they different? –  Tom J Nowell Apr 12 '13 at 20:52
    
You are querying the event_start_date and the event_end_time. What are you trying to achieve? –  vancoder Apr 12 '13 at 20:57
    
Tom J Nowell: Event id = 66, event_start_date = 2013-04-12 event_end_time = 22:00 –  Bryan White Apr 12 '13 at 21:00
    
vancoder: I want to pull the top 8 events that have not happened yet. They are not displayed if today's date is after event_start_date AND the current time is greater than event_end_time. So if today is April 12, 2013 at 8:00pm and the event is over on April 12, 2013 at 7:30pm the event won't be on the list. –  Bryan White Apr 12 '13 at 21:03
    
Just a quick side-comment. Why not just use "get_posts" instead of juggling all that stuff with the $wp_query variable? As far as the main question, my guess would be a mismatch with the time value stored in the DB compared to date('g:i A'). –  infamouse Apr 12 '13 at 21:09
show 4 more comments

1 Answer

You should be storing and comparing your time in 24 hour format: 00:00:00.

array(
'key' => 'event_end_time',
 'value' => date('G:i:s'),
 'compare' => '>=',
 'type' => 'TIME'
)
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.