WordPress WP_Query comparing dates in custom field - php

I am trying to run a WP_Query in WordPress to retrieve posts dated after today using a custom field. My key _mcd_event_date_end has values stored in the m/d/Y format (02/16/2016).
$args = array(
'order' => 'DESC',
'posts_per_page' => -1,
'post_type' => 'mcdevent'
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_mcd_event_date_end',
'value' => date("m/d/Y"),
'compare' => '>=',
),
array(
'key' => '_mcd_event_type',
'value' => 'Other Event',
'compare' => '=',
),
),
);
$other_events = new WP_Query($args);
wp_reset_postdata();
This is the code I am running, and it works for dates posted this year, but not prior. Meaning if I have a post with the _mcd_event_date_end key having a value of something like 4/16/2016 it will show up and if it is something like 2/12/2016 it will not show up. But then anything from 12/31/2015 and before also shows up.
Thanks for your help and please let me know if there's any more information I can provide.

You should be using "date" for the custom field type:
array(
'key' => '_mcd_event_date_end',
'value' => date('Y-m-d H:i:s'),
'compare' => '>=',
'type' => 'DATE'
),
But I think the data must be in "YYYY-MM-DD" format for it to work.
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

Related

Why doesn't the query return archived posts?

I am using the "Archived post status" plugin to make posts archive. Here is the link of this plugin: Archive post status
I’m trying to get archived posts between two dates: start date and end date, with wordpress query. The start date and end date comes from url, I'm taking them with $_GET. Here is my code:
$startDate = $_GET['startdate'];
$endDate = $_GET['enddate'];
$firstDayOfMonth = date(“Ym01”, strtotime($_GET[‘startdate’]));
$lastDayOfMonth = date(“Ymt”, strtotime($_GET[‘enddate’]));
$arguments = [
'posts_per_page' => 10,
'post_status' => 'archive',
'post_type' => 'agenda',
'meta_query' => [
'relation' => 'AND',
[
'key' => 'date',
'compare' => '>=',
'value' => $firstDayOfMonth,
],
[
'key' => 'date',
'compare' => '<=',
'value' => $lastDayOfMonth,
]
]
];
If I remove the meta query with start date and end date the query returns me archived posts, but with meta query, it doesn't return anything.
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'state',
'value' => 'Wisconsin',
),
array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
) );
do like this i hope this work.
with array()
Ref :- Link
'relation' => 'AND',
array(
'key' => '_my_custom_key_2',
'value' => 'Value I am looking for 2',
'compare' => '='
),
array(
'key' => '_my_custom_key_3',
'value' => 'Value I am looking for 3',
'compare' => '='
)
First, You compare string values(meta_query understand date key like string), not numbers, so You need compare meta value like number(for better accuracy in format date seconds from 1970 for example (use strtotime()) if possible).
To say WordPress that the meta value must be compare like it is number, just add parameters 'type' => 'NUMERIC' or 'type' => 'DECIMAL(10,3)'(for float numbers) .
Example of arguments array for You:
$args = [
'posts_per_page' => 10,
'post_status' => 'archive',
'post_type' => 'agenda',
'meta_query' => [
[
'key' => 'date',
'type' => 'DECIMAL(10,3)',
'compare' => 'BETWEEN',
'value' => array($firstDayOfMonth,$lastDayOfMonth)
]
]
];
Also, before put array value value of start and end date to be sure recommend use intval() for date var.

Sort event by start date but show until end date has passed on PHP query for Wordpress

I have a question about sorting upcoming events on a website with a PHP query. The issue i am having is that some events run over a couple of days. But I'd like that event to remain visible until the event end date has passed. Right now, once the event start date has passed, the event moves to the bottom of my event list. However, I'd like it to stay in the same chronological order of start date until the end date is over. Hope that makes sense! Any help would be so much appreciated. Here's my query...
<?php
$now = date('Y-m-d H:i:s');
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
'date_upcoming_clause' => array(
'key' => 'event_start_date',
'compare' => '>=',
'value' => $now,
'type' => 'DATETIME'
),
array(
'relation' => 'AND',
'date_started_clause' => array(
'key' => 'event_start_date',
'compare' => '<=',
'value' => $now,
'type' => 'DATETIME'
),
'date_end_clause' => array(
'key' => 'event_end_date',
'compare' => '>=',
'value' => $now,
'type' => 'DATETIME'
),
),
),
'orderby' => array(
'date_started_clause' => 'ASC',
'date_end_clause' => 'ASC',
'date_upcoming_clause' => 'ASC',
),
);
$the_query = new WP_Query($args);
?>

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.

Custom wordpress meta query (no result)

Problem
I am looping through custom post types (Advanced Custom Fields) in Wordpress. I only want to display events with start_date equal to $newdate variable, defined in the beginning.
The start_date is in the format YYYY-MM-DD HH:mm (the same as $newdate). $newdate is set to the beginning of the day so I wouldn't exclude events with different hours in the day and compare is set to greater than (just to test the query).
However I am not getting any results.
<?php
$newdate = date('Y-m-d 00:00');
//<-- Start the Loop. -->!
$args = array(
'post_type' => 'epsa_events',
'posts_per_page' => 5,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array (
array(
'key' => 'start_time',
'value' => $newdate,
'compare' => '>=',
'type' => 'datetime'
)
)
);
$loop = new WP_Query( $args );
Try this query:-
'meta_key' => 'event-start-date',
'meta_query' => array (
array(
'key' => 'start_time',
'value' => date('Ymd',strtotime($newdate)),
'compare' => '>=',
'type' => 'date'
)
)
I guess you have some small mistakes here.
First, if you use 'orderby' => 'meta_value' you must add a 'meta_key' => KEYVALUE to your $args where KEYVALUE is the name of the custom field you want to use for sorting. Check WP_Query documentation on Order & Orderby Parameters.
Second, assuming your start_date field type is Date Time Picker, if you want to get all events that already started you're using the wrong comparison operator. So assuming you also want to sort by date, your $args code should be:
$args = array(
'post_type' => 'epsa_events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'start_time',
'meta_type' => 'DATETIME'
'meta_query' => array (
array(
'key' => 'start_time',
'compare' => '<=',
'value' => $newdate,
'type' => 'DATETIME'
)
)
);
Take a look at Date Time Picker documentation too.
I hope this helps!

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

Categories