Support Forum
Dear Metabox. I'm trying to set up a comparison datetime, so as to only show current or future events posts. I'm failing and not sure what I'm doing wrong. I'm presuming the 'timestamp' => true, option has been added to metabox.io for precisely this particular sort of comparison, but I can't get it to work. Any help would be hugely appreciated.
My reasoning as follows:
1) the datetime meta_box field is set to save in the database with 'timestamp' option
array(
'name' => 'Date',
'desc' => 'For events over a stretch of time or taking place on more that one day, this should be the END DATE.',
'id' => $prefix . 'event-date',
'type' => 'datetime',
'timestamp' => true,
),
2) Second, my query
If my thinking is correct - this should return posts with a date equal to or greater than the current date - but no show of any posts.
<?php
$type = 'npl-event';
$key = 'poetrylibrary_event-date';
$today = current_time('timestamp');
$args = array (
'post_type' => $type,
'meta_key' => $key,
'meta_value' => $today,
'meta_query' => array(
'key' => $key,
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC'
)
);
$homequery1 = clone $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
/* Start the Loop */
while ( $wp_query->have_posts() ) : $wp_query->the_post();
etc…
I've just checked the database and the stored meta_value for the chosen meta_key (poetrylibrary_event-date) is 2016-04-15 - this doesn't look like a timestamp. Is my thinking wrong or have I applied the option in the wrong way?
I need to amend my previous comment - I hadn't refreshed, and now the timestamps are showing in the expected format in the database. However, I am still getting nothing working.
I have now got the timestamps comparing correctly by amending my query - which I had incorrectly added a meta_value => $today to. It is now showing posts as intended. HOWEVER - I am now struggling to change the timestamp meta_value to a front-end readable date. Currently only showing the 1970 date.
YAY: Using the following has delivered what I need. Please get rid of this thread unless of use.
$date = rwmb_meta( 'poetrylibrary_event-date', true ) ;
$dateoutput = date_i18n( 'jS F', $date);
Hi @lemonydesign, you're doing correctly. Glad that you figured it out so fast 🙂