0

I am having a weird situation, I have two meta keys in my custom posts. Start Date and End Date,

In simple SQL, I want to execute following query

where startDate > '2013-12-10' and (startDate <= '2013-12-10' and endDate >= '2013-12-10')

I am basically looking for posts which are between a range of startDate and endDate and should show further posts also.

I have tried this code, don't know whether its correct or not but tried and it didn't threw any error:

$args = array(
    'posts_per_page' => 100,
    'post_type' => 'create_events',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'startDate',
            'value' => date('Y-m-d'),
            'compare' => '>'
        ),
        array(
            'relation' => 'AND',
            array(
                'key' => 'startDate',
                'value' => date('Y-m-d'),
                'compare' => '<='
            ),
            array(
                'key' => 'endDate',
                'value' => date('Y-m-d'),
                'compare' => '>='
            ),
        ),
    'orderby'  => 'meta_value', 
    'meta_key' => 'startDate',
    'order' => 'DESC'
    )
);
query_posts($args);

Might be my logic be wrong,

1
1

First, the sample WHERE clause you provide doesn't make sense. That is not how to write a query that will return "between a range of startDate and endDate". Secondly...

I am basically looking for posts which are between a range of startDate and endDate and should show further posts also.

It is not clear what you mean by "show further posts also" but the only thing that makes sense to me is that you want to show posts outside your range-- presumably posts greater than startDate as in the first clause of your confusing sample WHERE. That being the case, all you need is:

$args = array(
    'posts_per_page' => 100,
    'post_type' => 'create_events',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'startDate',
            'value' => date('Y-m-d'),
            'compare' => '>'
        ),
    ),
    'orderby'  => 'meta_value', 
    'meta_key' => 'startDate',
    'order' => 'DESC'
    )
); 

You are correct that the arguments you were trying to use won't work. WP_Query is not capable of logic that complicated, but, as I've said, your sample WHERE doesn't make sense. Draw it on a number line and you should see what I mean. (startDate <= '2013-12-10' and endDate >= '2013-12-10') gives you almost the opposite of what your English description asks for.

Also...

Please don't use query_posts.

It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats for details.

http://codex.wordpress.org/Function_Reference/query_posts (emphasis mine)

3
  • Haha, of course you beat me to it..
    – Nicolai
    Dec 10 '13 at 14:59
  • @ialocin : not sure if I did... bit confused by the question :)
    – s_ha_dum
    Dec 10 '13 at 15:02
  • True, me too, I guess we made pretty much the same assumption, but you summarized it good, so now it should be solvable to the OP.
    – Nicolai
    Dec 10 '13 at 15:05
1

Your logic is wrong because you look for post that have greater start date and at the same time lesser or equal start date then today, so unless there is a event which has a start date of today, you won't get a result. Besides that your meta_query is wrong, additionally you have to change the start/end date compare, plus don't compare those to the same date, and last but not least don't use query_posts() use WP_Query instead.

$args = array(
    'posts_per_page' => 100,
    'post_type' => 'create_events',
    'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'startDate',
                'value' => date('Y-m-d'),
                'compare' => '>='
            ),
            array(
                'key' => 'endDate',
                'value' => date('Y-m-d'),
                'compare' => '<='
            ),
    'orderby'  => 'meta_value', 
    'meta_key' => 'startDate',
    'order' => 'DESC'
    )
);
$custom_query = new WP_Query( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.