Home › Forums › General Issues › Issue with query between 2 dates
Having an issue with query between start and end dates. Query works fine until I add in the meta-query. I have tried all solutions I can think of. Any ideas? Here is my code
[code]
$today = date(‘Ymd’);
$args = array(
‘post_type’ => ‘ads’,
‘categories’ => $category,
‘post_status’ => ‘publish’,
‘orderby’ => ‘rand’,
‘numberposts’ => ‘1’,
‘meta_query’ => array(
‘relation’ => ‘AND’,
array(
‘key’ => ‘start_date’,
‘compare’ => ‘>=’,
‘value’ => $today,
),
array(
‘key’ => ‘end_date’,
‘compare’ => ‘<=’,
‘value’ => $today,
)
)
);
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) :
// do some stuff
endif;
[/code]
This looks correct, the only thing I can see is that you have
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>=',
'value' => $today,
),
array(
'key' => 'end_date',
'compare' => '<=',
'value' => $today,
)
)
so only posts with today’s date in both the start and end date will appear. Maybe you’re not comparing it right. What do you want it to return?
Hi, same problem here. In my case what I want to achieve is, having events with start_date and end_date, check if any of them are active in a given date.
In other words, if that given date is between start_date and end_date of any of the events.
I tried something similar to your code @hube2 , but I get no results. I also tried with ‘OR’ relationship, but in that case is like showing all events, as start_date or end_date will match the rule.
This depends on what you’re looking for exactly.
Do you have a range of dates that the start and end date of the event needs to be between? Do you have a date and anything after that date is shown? These would be completely different queries.
For example, if I wanted all of the events with start or end dates after today, then I wouldn’t check the start date at all because if the end date is after today then the start date does not matter
'meta_query' => array(
array(
'key' => 'end_date',
'compare' => '>=',
'value' => $today,
)
)
But if I want events that are happening between two dates
'meta_query' => array(
'relation' => 'OR',
array(
array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>=',
'value' => $start_range
),
array(
'key' => 'start_date',
'compare' => '<=',
'value' => $end_range
)
)
),
array(
array(
'relation' => 'AND',
array(
'key' => 'end_date',
'compare' => '>=',
'value' => $start_range
),
array(
'key' => 'end_date',
'compare' => '<=',
'value' => $end_range
)
)
)
)
The topic ‘Issue with query between 2 dates’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.