Skip Navigation

[Resolved] What format has Toolset Date Field?

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

This thread is resolved. Here is a description of the problem and solution.

Problem:
I have a Custom Query where I check against a Toolset Date Field in a WP_Query.
Example:

$today = date( 'Y-m-d', strtotime('today') );

'meta_query' => array(
                                array(
                                    'key'     => 'wpcf-tour-date',
                                    'value'   => $today,
                                    'compare' => '>=',
                                    'type'    => 'DATE',
                                ),
                            ),

It does not return anythign, even if there are fields with values.

What's wrong?

Solution:
Our Dates Field is a Timestamp.
As such, it's a numeric value, not a date format.

The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYY-MM-DD and tested with this format.

And our Date Field is not storing it in this format but in a simple timestamp format.

The easiest solution is to compare as a NUMERIC value as that is what Toolset stores for the Date Fields.

Relevant Documentation:
https://toolset.com/forums/topic/filter-only-future-posts-in-wp_query-by-custom-date-field/#post-547754

This support ticket is created 7 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by daveM 7 years, 4 months ago.

Assisted by: Beda.

Author
Posts
#547626

I have a WP_Query with the following meta_query args:

$today = date( 'Y-m-d', strtotime('today') );
						
					foreach ( $tourDateArtists as $tourDateArtist ) {						
						$tourDateArtist_query = new WP_Query( array(
							'post_type' => 'tour-date',
						    // 'posts_per_page' => -1,
						    'meta_key'     => 'wpcf-tour-date',
							'orderby'      => 'meta_value_num',
							'order'        => 'ASC',
							'cat'          => '-1',
							'tax_query' => array(
						        array(
						            'taxonomy' => 'category',
						            'field'    => 'slug',
						            'terms'    => array( $tourDateArtist->slug ),
						            'operator' => 'IN',
						        ),
					        ),
					        'meta_query' => array(
								array(
									'key'     => 'wpcf-tour-date',
							        'value'   => $today,
							        'compare' => '>=',
							        'type'    => 'DATE',
								),
					        ),
						));

if ( $tourDateArtist_query->have_posts() ) {
							echo '<div id="' . $tourDateArtist->slug . '" class="has-dates">';
							echo '<h2>' . $tourDateArtist->name . '</h2>';
						    echo '<ul class="' . $tourDateArtist->slug . '-tour-dates">';
					    	while ( $tourDateArtist_query->have_posts() ) {
						    	$tourDateArtist_query->the_post();
						    	echo '<li class="tour-date-item">';
						    	echo '<a href="' . types_render_field( 'tour-date-link' ) . '">';
						    	echo '<div class="tour-date-date">' . types_render_field( 'tour-date', array( 'format' => 'F j, Y g:i a') ) . '</div>';
						    	echo '<div class="tour-date-city-state">' . types_render_field( 'city-state' ) . '</div>';
						    	echo '<div class="tour-date-venue">' . get_the_title() . '</div>';
						    	echo '</a>';
						    	echo '</li>';
						    }
						    echo '</ul>';
							echo '</div>';
						    
						    // echo ('<a href="/tour-dates#' . $parentSlug . '" class="load-more-link">Show More Dates</a>');
						
					    } else {
							/* show 'no news' message if no tour dates */
							echo '<div id="' . $tourDateArtist->slug . '" class="no-dates">';
							echo '<h2>' . $tourDateArtist->name . '</h2>';
							echo '<p>' . $tourDateArtist->name . ' is not on tour.' . '</p>';
							echo '</div>';
						}
						$tourDateArtist_query = null;
						wp_reset_postdata();
					}

The problem is that when the 'meta_query' is included, the loop fails and the 'else' statement is fired. When the 'meta_query' is not included, the loop loads as expected(except for the 'cat' => '-1' not excluding that category correctly).

This is the WordPress Codex page explaining the 'meta_query' args: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

It seems like the Types Custom Field key I'm using for 'meta_key' with 'orderby' works fine, but when it's in 'key' for the 'meta_query' it fails.

Any idea what I'm doing wrong? It seems to match the Codex and many other examples I've found exactly. Thanks!

#547754

We do not assist Custom PHP coding in the Free Types Forum.

We could if it would use the Types API, but the Types API does not include Queries.

This is a topic of Views and for that, we have an API that we can partially assist:
https://toolset.com/documentation/programmer-reference/views-filters/

Now, related to your issue, I see some possible points:

1. Our Dates Field is a Timestamp.
As such, it's a numeric value, not a date format.

The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYY-MM-DD and tested with this format.

And our Date Field is not storing it in this format but in a simple timestamp format.

2. You compare to a value $today, wich is created by:

date( 'Y-m-d', strtotime('today') );

This returns a string formatted according to the given format string using the given integer timestamp, in your case from strtotime('today') it makes a Y-m-d Date.

So, with strtotime you get a Timestamp, but then you convert it to a Date in format Y-m-d, and then compare that to the Field value, which is again a Timestamp.

That will never match.

As far I get your code, you should be able to simply do:
$today = strtotime('today') );

That should give you a Timestamp and that is comparable as NUMERIC in the field's value.

#547917

Thanks, changing the 'type' => 'DATE' to 'type' => 'NUMERIC' and the $today variable to what you mention above made it work!

Finding out that the Types "Date" field is not a date format is really what my post was about. I know the rest is somewhat unrelated, so thanks for looking into it!

The forum ‘Types Community Support’ is closed to new topics and replies.