Datetime comparison issue, timestamp set to true

Support General Datetime comparison issue, timestamp set to true

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2783
    lemonydesignlemonydesign
    Participant

    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…
    #2784
    lemonydesignlemonydesign
    Participant

    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?

    #2785
    lemonydesignlemonydesign
    Participant

    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.

    #2787
    lemonydesignlemonydesign
    Participant

    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.

    #2788
    lemonydesignlemonydesign
    Participant

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

    #2793
    Anh TranAnh Tran
    Keymaster

    Hi @lemonydesign, you're doing correctly. Glad that you figured it out so fast 🙂

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Datetime comparison issue, timestamp set to true’ is closed to new replies.