Datetime comparison issue, timestamp set to true
- This topic has 5 replies, 2 voices, and was last updated 9 years ago by
Anh Tran.
-
AuthorPosts
-
April 15, 2016 at 7:46 PM #2783
lemonydesign
ParticipantDear 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' optionarray( '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…
April 15, 2016 at 8:07 PM #2784lemonydesign
ParticipantI'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?
April 15, 2016 at 8:19 PM #2785lemonydesign
ParticipantI 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.
April 15, 2016 at 9:04 PM #2787lemonydesign
ParticipantI 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.
April 15, 2016 at 9:12 PM #2788lemonydesign
ParticipantYAY: 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);April 15, 2016 at 11:51 PM #2793Anh Tran
KeymasterHi @lemonydesign, you're doing correctly. Glad that you figured it out so fast 🙂
-
AuthorPosts
- The topic ‘Datetime comparison issue, timestamp set to true’ is closed to new replies.