Skip Navigation

[Resolved] WP Query – Filtering by Date Field

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by calgaryA 5 years ago.

Assisted by: Nigel.

Author
Posts
#1394583
CA-Events.jpg

I'm trying to create a WP Query that displays posts from a custom post type (ca-event). In the custom post type, I'm using the date field to capture the date only.

I'd like to only display the posts that have a date that has not past (so, >= today's date). After reviewing some comments from your forums they indicated that the date field is a timestamp. However, when I do a var_dump on my date field, it outputs as a string. I've attached a screenshot from my localhost for reference.

This is my query so far.... but because the date field is outputting as a string rather than a timestamp... this is obviously not working.

$today = strtotime('today');

$ca_event_args = array(
'post_type' => 'ca-event',
'post_status' => 'publish',
'posts_per_page' => -1,
'event_category' => array(
'key' => 'wpcf-event-category',
'value' => $event_category,
),
'event_date' => array(
'key' => 'wpcf-event-date',
'value' => $today,
'compare' => '>=',
'type' => 'MUMERIC'

),
'orderby' => 'event_date',
'order' => 'DESC',

);

Help!

#1394825

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Is your event date field actually a Types date field?

Because Types date fields certainly are stored as timestamps, which you can verify by checking wp_postmeta, noting that the key includes a 'wpcf-' prefix.

You have that in your query args, but where does the var_dump('event-date') in your screenshot come from? It's not a variable, obviously.

#1394829
event-date.jpg

Yes, the event date field is a types date field (see attached screenshot. Sorry, correction to the var_dump. It's a var_dump(types_render_field('event-date')), after the query is run.

Just checked my database... not sure why I cannot locate wpcf-event-date anywhere. However, I have 4 posts using that custom field.... any insights on why that would be??

#1394901
ca-event-date.jpg

Ignore my previous comment about not finding the wpcf-event-date meta key in my table. I wasn't viewing all the rows in my table. It is storing the values as timestamps, not sure why the var_dump is displaying it as a string though....

#1395067

My issue is resolved now. Thank you!