WP_Query exlude posts where meta_key => DeletedDate is older than - php

I created a custom post_type and need to configure my WP_Query a bit in archive.php.
In my archive.php I would like to retrieve all posts that do NOT have a date in meta_key => DeletedDate and is older than 2 weeks.
So, if a car has a date in meta_key => DeletedDate that is older than 2 weeks from today, it should be excluded from the WP_Query
My $args:
$today = strtotime('-1 weeks');
$args=array(
'posts_per_page' => -1,
'post_type' => 'vehicle',
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'DeletedDate',
'value' => $today,
'compare' => '<=',
),
array(
'key' => 'DeletedDate',
'value' => null,
'compare' => '=',
)
),
);
query_posts($args);
Hope it makes sense.

Related

Custom post type upcoming events for week and next month

$todayDate = date('m/d/Y');
$futureDate = strtotime ( '+7 days' , strtotime ( $todayDate ) ) ;
$futureDate = date ( 'm/d/Y' , $futureDate );
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'event_category' => 'events',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $futureDate,
)
),
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);
I want to show the upcoming events for the next week or next month. I have this args array set but still giving me the post of yesterday's and not sorted as how i wanted. Also, how to get rid of yesterday's post in my query..
So if you want your args to limit events that fall between today and the future date, where future date is today +7 days.
Hence you want to modify your args to include another array with the following parameters. Try this:
//event_date >= $todayDate
//event_date <= $futureDate
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'event_category' => 'events',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $todayDate,
),
array(
'key' => 'event_date',
'compare' => '<=',
'value' => $futureDate,
),
),
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);

WordPress: Query posts by date range

I'm working on pages in a WordPress site to show "Upcoming" and "Archived" events by date range using custom fields for event_start_date and event_end_date with the custom post type event. I'd like the "Upcoming" page to show posts for which the current date falls within this range and/or equals the start date or end date; and the "Archive" page to show events for which the current date is after the date range entirely. Thus, if today is October 10, and an event with the dates October 9-11 or October 10-12 would show on "Upcoming" and an event with any previous dates would show on the "Archived" page.
I'm including my queries below for both the upcoming and archive pages; the query for upcoming seems to be working ok, but in the archive page if the current date falls within the date range it will still show, and I'd like it to only show past events:
/* upcoming events query */
$event_args = array(
'post_type' => 'event',
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => get_query_var( 'paged' ),
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'event_start_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '>=',
),
array(
'key' => 'event_end_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '>=',
),
),
);
/* archived events query */
$event_args = array(
'post_type' => 'event',
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => get_query_var( 'paged' ),
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'event_start_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '<',
),
array(
'key' => 'event_end_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '<',
),
),
);
Thus, the meta_query for the upcoming events query seems to be working for all cases, but that for the archive page is not behaving as intended. Thank you for any assistance here, and please let me know if my question is unclear in any way. EDIT I initially neglected to mention that I also need to account for the scenario in which only a start date is entered, i.e. for single-day events, which is proving to be a sticking point for the archive page query.
In your archive query, you are using two meta_query conditions, I think the issue is with the first condition
First condition: event_start_date
array(
'key' => 'event_start_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '<',
),
This will query all events if the event_start_date is less than current_time. Also consider that you are creating an OR relation.
Now, let's say today is Oct 10 and an even spans from Oct 9 to Oct 12, the above meta_query will include this event as well because the event_start_date is less than current_time. Make sense?
The solution will be to include meta_query for event_end_date only. This way, only events with ending date less than current_time will be queried.
/* archived events query */
$event_args = array(
'post_type' => 'event',
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => get_query_var( 'paged' ),
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_end_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '<',
),
),
);
Update:
Nested meta_query logic to handle single day past events as well.
/* archived events query */
$event_args = array(
'post_type' => 'event',
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => get_query_var( 'paged' ),
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_start_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '<',
),
array(
'relation' => 'OR',
array(
'key' => 'event_end_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '<',
),
array(
'key' => 'event_end_date',
'type' => 'DATE',
'value' => NULL,
'compare' => '=',
),
)
),
);

Query by start date and then by start time in Wordpress

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',

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'
),
),
);

How to combine meta_query and orderby date meta in WordPress?

I have 3 meta fields, date (date formatted string), location, and consultant.
I can pull the posts and orderby the date field when meta_key is set:
// get posts
$posts = get_posts(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
However, I have not be able to get this to work with a more complex meta_query:
$args = array(
'post_type' => 'uppy_events',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => $key,
'value' => array($value),
'compare' => '>='
)
),
'numberposts' => -1
);
Basically I want to pull all posts that have a meta start_date greater than today's date and order by these dates.
As a side note - can you do a meta_query search on say location key and still orderby date key?
This is the first time I've ever tried ordering by meta, and have found some good posts but still cannot wrap my head around it.
OK, I figured this out as my post lead me to another good example. The commented out meta_query is date greater than today. The other is using a different key. Both work!
$today = date("Ymd");
$args = array(
'post_type' => 'uppy_events',
'post_status' => 'publish',
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
/* array(
'key' => 'event_date',
'value' => $today,
'compare' => '>='
) */
array(
'key' => 'event_location',
'value' => 'vancouver',
'compare' => '='
)
),
'numberposts' => -1
);

Categories