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 feel like I'm extremely close on this one but the query keeps on showing up empty. Basically, I'm trying to query the custom values between two dates. Records should be returned if the start date is less than the current date and the end date is greater than or equal to the current date:

$args = array(
    'taxonomy' => 'exhibition_type', 
    'term' => 'faculty',
    'numberposts' => 10,
    'post_type' => 'exhibitions',
    'meta_key' => 'start_date_of_event',
    'meta_query' => array(
        'relation' => 'AND',
            array(
                'key' => 'start_date_of_event',
                'value' => $current,
                'compare' => '<'
            ),
            array(
                'key' => 'end_event_date',
                'value' => $current,
                'compare' => '>='
            )
        ),
    'order_by' => 'meta_value_num',
    'order' => 'ASC',
    'paged' =>  $paged
);

The code I'm using now keeps on turning up 0 results and I'm stuck at the moment. Any help would be much appreciated.

share|improve this question
    
Please remove the 'meta_key' => 'start_date_of_event' and the 'order_by' => 'meta_value_num' and tell me if it's working then. –  kaiser Sep 4 '12 at 17:22
    
Can we see what form the value of $current takes? –  Tom J Nowell Sep 4 '12 at 21:49
1  
nm saw your reply to sanchos, if this worked, how many posts would you see? What happens if you remove all the other filters like taxonomy etc and just specified the post type and the meta query? –  Tom J Nowell Sep 4 '12 at 22:06
    
Read this thread on more info about querying with two conditions: ottopress.com/2010/wordpress-3-1-advanced-taxonomy-queries/… –  developdaly Sep 6 '12 at 3:37
1  
Have you tried BETWEEN instead of 2 conditions? –  Rilwis Sep 6 '12 at 6:49
add comment

2 Answers

up vote 4 down vote accepted
+100

Update - it may be supported in a meta_query. I need more information - see below.

You can't do it using a meta_query, it's not supported.

Do you have data in meta_data fields? - WordPress queries will exclude posts where any of the orderby (or meta_query) fields is missing. WordPress adds a join condition to the SQL query to support the orderby or query fields, an inner join (a join which only includes posts with the join field.)

If the the orderby 'inner join' is the issue, if you need to include posts where this field may be missing, then you can filter the join and orderby clauses of the generated SQL

If you're still not getting results, it would be useful to see the actual SQL that's being generated. Add

<pre><?php echo your_query_object->request; ?></pre> 

to your template to display it, and add it to your question as an update.

share|improve this answer
    
` ["_edit_last"]=> array(1) { [0]=> string(1) "1" } ["_edit_lock"]=> array(1) { [0]=> string(12) "1346276353:1" } ["start_date_of_event"]=> array(1) { [0]=> string(8) "20120912" } ["_start_date_of_event"]=> array(1) { [0]=> string(19) "field_5023c0cdaf93e" } ["end_date_of_event"]=> array(1) { [0]=> string(8) "20130113" } ["_end_date_of_event"]=> array(1) { [0]=> string(19) "field_502e5fc3879e9" } ["time_of_event"]=> array(1) { [0]=> string(16) "11:00am - 6:00pm" }` –  Robert Sep 4 '12 at 23:18
    
is output when Im not using that query. (this is an example of one post) –  Robert Sep 4 '12 at 23:19
    
The query you posted references the field 'end_event_date', but in your post data the field is called 'end_date_of_event'. If no post has the field 'end_event_date', no post would be returned. Change the key name and see if it works now. –  marfarma Sep 5 '12 at 14:03
    
............................... and the 100 bounty goes to you. Ive been trying to figure this out for two weeks and end_date_of_event was end_event_date... smh. Im sorry for wasting your time lol –  Robert Sep 5 '12 at 14:25
    
Seeing errors like that in your own code is just hard. Believe me, we've all been there. –  marfarma Sep 5 '12 at 15:19
show 1 more comment

Have you tried specifying the 'type' of data for the meta_query? I'm assuming you're using timestamps because I've written exactly this before now and have it working.

Eg:

$args = array(
    ...
    'meta_query' => array(
        'relation' => 'AND',
            array(
                'key' => 'start_date_of_event',
                'value' => $current,
                'compare' => '<',
                'type' => 'NUMERIC'
            ),
            array(
                'key' => 'end_event_date',
                'value' => $current,
                'compare' => '>=',
                'type' => 'NUMERIC'
            )
        ),
    ...
);

It'd be awesome if WP_Query supported sorting on different meta data types other than strings or numbers then you could actually use proper DATE and DATETIME comparisons :(

share|improve this answer
    
+1 Ad comparison) Won't happen, as WPs built in $wpdb->prepare() is equal to sprintf() (and similar) and therefore only does %s and %d. –  kaiser Aug 30 '12 at 16:53
    
No dice still an empty query. When I comment out the relation and the 2nd array it works but thats just one query lol. My $current = date('Ymd'); –  Robert Aug 30 '12 at 17:42
    
What do your event_end_date and event_start_date meta values look like? –  sanchothefat Aug 31 '12 at 13:30
    
Also your query logic will return currently running events that have started but not ended. Is that right? –  sanchothefat Aug 31 '12 at 13:42
    
@sanchothefat yes that is right. Also event start and end are also in the Ymd date format –  Robert Sep 4 '12 at 12:13
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.