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'm starting with Wordpress, already achieved some queries, but this one... ugh! It's being a pain to me!

What i want to do is this part of query:

" ... WHERE "VALUE_THAT_I_WANT" BETWEEN event_start_date AND event_end_date ..."

Where "event_start_date" and "event_end_date" are meta keys ( i want to use their values, not mine!)

This is what i had so far..

$tEA = array(
    'post_type' => 'event',
    'meta_key' => $metaKey,
    'orderby' => 'meta_value',
    'order' => $order,
    'posts_per_page' => $postPerPage,
    'meta_query' => array(
        array(
            'key' => $metaKey,
            'value' => $today,
            'compare' => "=",
            'type' => 'DATE'
        ),
        array(
            'key' => '_event_end_time'
        )
    )
);
share|improve this question

2 Answers 2

Didn't test it, but what about this?

$tEA = array(
    'post_type' => 'event',
    'meta_key' => $metaKey,
    'orderby' => 'meta_value',
    'order' => $order,
    'posts_per_page' => $postPerPage,
    'meta_query' => array(
        array(
            'key' => 'event_start_date',
            'value' => 'VALUE_THAT_I_WANT',
            'compare' => '>='
        ),
        array(
            'key' => 'event_end_date',
            'value' => 'VALUE_THAT_I_WANT',
            'compare' => '<='
        ),
        array(
            'key' => '_event_end_time'
        )
    )
);

I am not sure if you needed the other compare, if you do, just prepend it :P

share|improve this answer
    
in theory should work.. but its not =( I'm trying to fix this. If anyone know how to do it, please, feel free to post it! –  Hor Oct 30 '13 at 19:06
    
have you tried with only event_start_date array? Why it doesn't work? (no results?) –  Toni Michel Caubet Oct 30 '13 at 19:18
    
Yes, no results. Thats what im talking, in theory, this should work.. but does not! hehe! –  Hor Oct 31 '13 at 16:36

This should work for you:

$tEA = array(
    'post_type' => 'event',
    // you dont need meta_key as you use meta_query
    //'meta_key' => $metaKey,
    'orderby' => 'meta_value',
    'order' => $order,
    'posts_per_page' => $postPerPage,
    'meta_query' => array(
        array(
            'key' => $metaKey,
            // value should be array of (lower, higher) with BETWEEN
            'value' => array('START_DATE', 'END_DATE'),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        ),
    )
);

Note that START_DATE and END_DATE should be in MySQL date format YYYY-MM-DD

share|improve this answer
    
+1 but I'm pretty sure UNIX timestamps are perfectly ok. –  kaiser Oct 31 '13 at 0:32
    
@kaiser Thanks, but as the data type is casted to DATE, I am not sure if WP will format it properly while constructing the meta_query, so it is safer to directly use the MySQL date format in this case. –  Ahmad M Oct 31 '13 at 10:54
    
Ahmad, thanks for the answer, but in my problem 'START_DATE' and 'END_DATE' should be the meta_value of these fields. I have an event that has "_event_start_date" and "_event_end_date", and they are meta_keys. What i want is do a "BETWEEN" with these two meta_values. I dont want to insert manually the values, because thesse values are meta_values. Hehe.. –  Hor Oct 31 '13 at 16:35
    
If i do what you saying, look what the query prints: "BETWEEN '_event_start_date' AND '_event_end_date')" –  Hor Oct 31 '13 at 16:39

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.