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've searched WP Codex and StackExchange and gotten some clues, but I can't get this query working. I have 2 custom fields associated with each exhibition in Y-m-d format: exstart-date being the start date of the exhibition and exend-date being the ending date.

I can easily display upcoming exhibits and past exhibits, but I cannot get the meta_query right to display current exhibits (with a start date less than or equal to today AND end date greater than or equal to today). The code below displays nothing on the page. Help?

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$today = date('Y-m-d', strtotime('-6 hours'));
query_posts(array(
    'post_type' => 'exhibitions', 
    'posts_per_page' => 6, 
    'paged' => $paged,
    'orderby' => 'title',
    'order' => 'DESC',
    'meta_query'=>array(
        'relation'=>'AND',
        array(
            'key' => 'exstart-date',
            'value' => $today,
            'compare' => '<=',
            'type' => 'CHAR'
            ),
        array(
            'key' => 'exend-date',
            'value' => $today,
            'compare' => '>=',
            'type' => 'CHAR'
            )
        )
    ));
if (have_posts()) :
while (have_posts()) : the_post();
share|improve this question
    
one thing that immediately jumps out is 'type' => 'CHAR', should be 'type' => 'DATE', no? –  Milo Sep 11 '11 at 23:20
    
I tried using DATE and it still didn't work. When I do the query for only the start date or only the end date, CHAR works fine. –  Ray Gulick Sep 12 '11 at 1:50
    
the only other thing is that caller_get_posts is a true/false parameter, but has been replaced with ignore_sticky_posts, though that shouldn't really affect the query, you just get a deprecated notice. I tried it here and it seems to be working, maybe verify that nothing else is interfering with the query, like a plugin, print_r($wp_query) and inspect the contents. –  Milo Sep 12 '11 at 2:45
    
Thanks, but print query did not reveal anything. Something is wrong with the relational meta_query. If I query exstart_date or exend_date by themselves, no problem; I get the right result. But the combined query returns nothing. –  Ray Gulick Sep 12 '11 at 15:01
    
if something is wrong with the query, you'd see it in the generated SQL when you inspect $wp_query. it works as-is for me when I just copy/paste your code and add the proper meta fields (i'm also changing the post_type to one I already have set up). are your meta field dates formatted yyyy-mm-dd? I don't imagine the other queries you're doing would work if they weren't. –  Milo Sep 12 '11 at 15:18
show 6 more comments

1 Answer 1

up vote 1 down vote accepted

Here is the code I ended up with that works. I should have mentioned that the query was inside the loop, because when I showed it to Damian Taggart of Mindshare Studios, he noticed that he said I should be using WP_Query instead of query_posts. Thanks to Milo and others for attempting to help me without having all the necessary info.

<?php
     $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
     $today = date('Y-m-d', strtotime('-6 hours'));
     $myquery = new WP_Query(array(
     'post_type' => 'exhibitions', 
     'posts_per_page' => 6,  
     'paged' => $paged,
     'orderby' => 'title',
     'order' => 'ASC',
     'meta_query'=>array(
            'relation'=>'AND',
            array(
                'key' => 'exstart-date',
                'value' => $today,
                'compare' => '<=',
                'type' => 'CHAR'
            ),
            array(
                'key' => 'exend-date',
                'value' => $today,
                'compare' => '>=',
                'type' => 'CHAR'
            )
        )
    ));
    if ($myquery->have_posts()) :
    while ($myquery->have_posts()) : $myquery->the_post();
?>
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.