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 have a custom post type called Events, with 2 custom fields, start date and end date. These fields are stored using a timestamp in the database. I want to create a custom query based on these 2 values, so i can list events that are inside the specified date/time range. This is how my code looks like:

$start_date = strtotime($_POST['start_date']);
$end_date = strtotime($_POST['end_date']);
$args = array(
    'post_type' => 'event',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query'=>array(
        'relation'=>'AND',
         array(
            'key' => 'event_start_date',
            'value' => $start_date,
            'compare' => '<=',
            'type' => 'NUMERIC'
         ),
         array(
            'key' => 'event_end_date',
            'value' => $end_date,
            'compare' => '>=',
            'type' => 'NUMERIC'
         )
     )
);
$events = new WP_Query($args);      

So i'm using a simple NUMERIC meta_query, since all of my values and keys stored in a timestamp. The code looks fine for me, but somehow theres no results in this query. Here is an example:

$star_date = 1343779200 $end_date = 1412121600

And one of my event post has these values as a custom field: event_start_date = 1375315200 event_end_date = 1377734400

So it should give me at least one result, because the start_date is smaller and the end_date is higher compared to the event_start_date and event_end_date.

Any idea whats wrong?

share|improve this question
3  
I think you have used wrong comparison operators in meta query.It should be >= for 'event_start_date' and <= for 'event_end_date' –  Vinod Dalvi Aug 24 '13 at 10:21
    
Of course, thanks… Can you write this as the answer, so i can accept it? –  passatgt Aug 24 '13 at 19:03

2 Answers 2

up vote 0 down vote accepted

I think you have used wrong comparison operators in meta query .It should be >= for 'event_start_date' and <= for 'event_end_date' .

share|improve this answer
$today2 = date('Ymd');  
    $current_month = date('m');
    $current_year = date('Y');
    $today1 = $_GET['event_date'];
    //echo $xx = substr($today1,0,4);

    $current_month = substr($today1,-4,2);
    $current_year = substr($today1,0,4);

    //echo 'get-->'.$today1; 
    //$today = ($today1 == '' || NULL )? $today2 : $today1; 

    $args = array(

        'post_type' => 'aw_events',
        'posts_per_page'  => $posts_per_page,
        'paged' => $paged,
        'meta_query'   => array( 
            //'relation' => 'AND',              
            array(
                'key'   => 'event_start_date',
                'compare'   => '<=',
                'value'   => $current_year.''.$current_month.'30',

            ),
            array(
                'key'   => 'event_end_date',
                'compare'   => '>=',
                'value'   => $current_year.''.$current_month.'01',

            ),
        ),      

    );
share|improve this answer
3  
Please add an explanation to your answer: why could that solve the problem? –  toscho Jul 28 at 9:56

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.