Query by start date and then by start time in Wordpress - php

I am trying to query the database and ordering the result by the date and then by time. But with the query below it only orders by the start time. When I remove the start time it orders the result by date.
So far I have tried changing the order and multiple solutions throughout the web but nothing works.
$args = array(
'numberposts' => -1,
'post_type' => 'reservation',
'cat' => $categorie,
'meta_key' => 'start_time',
'orderby' => array( 'start_date' => 'ASC', 'meta_value_num' => 'ASC' ),
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>=',
'value' => $date,
),
array(
'key' => 'reservation_status',
'compare' => '<',
'value' => 3,
)
)
);
// query
$the_query = new WP_Query( $args );
Please help.
Thanks

How about something like this:
order => 'start_date start_time',
orderby => 'ASC',

Related

Wordpress Query with Multiple meta_keys not working

Ok, so I am querying events made by The Events Calendar, using Advanced Custom Fields. I have a plugin that converts the serialized data to standard under a new meta_key. That said, I am able to query events by the meta_key and meta_value individually. i.e
$args = array(
'numberposts' => -1,
'post_type' => 'tribe_events',
'meta_key' => 'display_override',
'meta_value' => 'Arkansas Literary Festival'
);
and also
// args
$args = array(
'numberposts' => -1,
'post_type' => 'tribe_events',
'meta_key' => 'display_global',
'meta_value' => 'Enabled'
);
However, I can not get them to work simultaneously, i.e
// args
$args = array(
'numberposts' => -1,
'post_type' => 'tribe_events',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'display_override',
'value' => 'Arkansas Literary Festival',
'compare' => '='
),
array(
'key' => 'display_global',
'value' => 'Enabled',
'compare' => '='
)
)
);
When I experiment with this, by using 'OR' or 'AND', and 'LIKE' instead of '=', I either get no posts, or I get the master list of unfiltered posts....

Don't display WordPress posts if time has passed using WP_Query

I have a custom post type of schedule which I can add 'events' with a custom field of Timeslot. This custom field is a repeater using ACF Pro.
It contains a date, start time and end time. Date picker and time pickers respectively.
On the homepage I have an image slider that pulls in the artwork and event title and only display 'todays' events, in time order.
What I want to do now is to not show events that have passed. So if the current time is 14:00pm for example, the all shows that ended before that time will not show. If the show started at 13:00pm and ends at 15:00pm though, that show will still show, and will appear first. I hope that makes sense?!
Here is my WP_Query so far but after a few attempts, I cannot figure out how to modify it to work as required:
<?php
$args = array(
'post_type' => 'schedule',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_date',
'compare' => '=',
'value' => $day,
),
'time_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_start_time',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'date_clause' => 'ASC',
'time_clause' => 'ASC',
)
);
<?php
$args = array(
'post_type' => 'schedule',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_date',
'compare' => '=',
'value' => $day,
),
'time_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_start_time',
'compare' => '>=',
'value' => time(),
),
),
'orderby' => array(
'date_clause' => 'ASC',
'time_clause' => 'ASC',
)
);
Note the change in the time_clause argument. So only show time_clause when it equals or is later than the time now.

Wordpress post query on more than one condition

I have the following function which returns only future events, which works great:
$args = array(
'post_type' => self::POST_TYPE,
'posts_per_page' => $posts_per_page,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'status' => 'publish',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
)
)
);
The problem I have is, I also need to check whether a custom field called "post_is_global" has been set (the type is BOOL by the way) but I don't know how to implement it into this query. Any help would be greatly appreciated.
Many thanks!
The query should look somewhat like this:
$args = array(
...
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
),
array(
'key' => 'post_is_global',
'value' => '1',
'compare' => '=',
),
)
);
$query = new WP_Query($args);
References:
ACF | Query posts by custom fields
Class Reference/WP_Query

Date issue in custom post type loop (wordpress)

I am using a custom post type with custom date fields to display tour dates on a page. Some of the posts have both a "from" and "to" date (because they last for more than one day) and some only have "from" (because they're only one day), and I want all future or current tour dates to be displayed.
It's all working perfectly, except for some reason the posts that only have a "from" date stop showing if that date is in 2016 or later. It doesn't make any sense to me!
Here are the args:
$today = date('Ymd');
$tour_args = array(
'post_type' => 'tour_date',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'tour-date-from',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'tour-date-from',
'value' => $today,
'compare' => '<=',
),
array(
'key' => 'tour-date-to',
'value' => $today,
'compare' => '>=',
),
),
);
This is now solved. The problem was that I needed to specify the meta type. I also needed to take out the 'meta_key' line and therefore amend the 'orderby' line to look for the right value.
$today = date('Ymd');
$tour_args = array(
'post_type' => 'tour_date',
'posts_per_page' => -1,
'orderby' => 'tour-date-from',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'tour-date-to',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC'
),
array(
'key' => 'tour-date-from',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC'
),
),
);

Compare Two meta_values in wordpress

I feel like im extremely close on this one but the query keeps on showing up empty. Basically, Im trying to query the custom values between two dates. if the start date is less than the current date AND the end date is greater than or equal to the current date
$args = array(
'taxonomy' => 'exhibition_type',
'term' => 'faculty',
'numberposts' => 10,
'post_type' => 'exhibitions',
'meta_key' => 'start_date_of_event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date_of_event',
'value' => $current,
'compare' => '<'
),
array(
'key' => 'end_event_date',
'value' => $current,
'compare' => '>='
)
),
'order_by' => 'meta_value_num',
'order' => 'ASC',
'paged' => $paged
);
keeps on turning up 0 results :'( I'm stuck at the moment. Any help would be much appreciated.
its possible. I entered my data in wrong :D
Here is the working code:
$args = array(
'taxonomy' => 'exhibition_type',
'term' => 'faculty',
'numberposts' => 10,
'post_type' => 'exhibitions',
'meta_key' => 'start_date_of_event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date_of_event',
'value' => $current,
'compare' => '<'
),
array(
'key' => 'end_date_of_event',
'value' => $current,
'compare' => '>='
)
),
'order_by' => 'meta_value_num',
'order' => 'ASC',
'paged' => $paged
);
This code uses two meta_values (start_date_of_event & end_date_of_event) and compares that to the current date which is ( $current = date('Ymd'); ). Hope this helps others. :D
If I understand you I think this is your answer,
if(strtotime($args['meta_query'][0]['key']) > strtotime($args['meta_query'][1]['key'])){
//do stuff here
}

Categories