Support

Account

Home Forums General Issues Filtering by date

Solved

Filtering by date

  • Hello!

    I’m at my wit’s end on an issue about filtering a custom post type by a datepicker field.

    I’ve looked up previous answers here in the forum and I seem to have everything that is suggested, but I always get 0 posts retrieved despite having posts that should be matching the criteria.

    Currently, my code is as follows:

    $today = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
    echo $today;
    
    $args = array(
    	'post_type' => 'shows',
    	'posts_per_page' => -1,
    	'meta_key' => 'event_date',
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    	'meta_query' => array(
    	    array(
    	      'key' => 'event_date',
    	      'value' => $today,
    	      'type' => 'DATE',
    	      'compare' => '>',
    	    )
    	)
    );
    
    $the_query = new WP_Query( $args );
    
    if ( $the_query->have_posts() ) {
     // display
    }
    else {
     echo "no results found";
    }
    
     wp_reset_query();

    When I take the meta_query piece out of my query, it displays all of my posts accordingly.

    Hoping that someone can see something that I can’t… any help would be appreciated.

  • Hi @AdamWebDev

    For reference, the date field docs are located here:
    http://www.advancedcustomfields.com/resources/field-types/date-picker/

    I have not yet used the DATE compare method, and can’t confirm that it works with a custom field value in the format of Y-m-d.

    To sort posts based on a date picker value, the value must be saved with the format yymmdd. Is this the save format you are using? Or are you using something else?

    Your next issue is that $today is in a format of Y-m-d. These ‘-‘ characters need to be removed and you need to sort by Ymd

    Then change your compare type from DATE to NUMERIC, however you may not even need this setting.

    Hope that helps.

    Thanks
    E
    Basically, you need to c

  • So, I was using the Date Time picker plugin, which seemed to be causing issues. Since there didn’t seem to be any documentation on filtering via that plugin, I just went back to the plain date picker, and my issue was resolved.

  • I’m late to the party, but I arrived here after looking for an answer I knew existed.

    The answer can be found here.

    <?php
    
    // find date time now
    $date_now = date('Y-m-d H:i:s');
    
    // query events
    $posts = get_posts(array(
    	'posts_per_page'	=> -1,
    	'post_type'			=> 'event',
    	'meta_query' 		=> array(
    		'relation' 			=> 'AND',
    		array(
    	        'key'			=> 'start_date',
    	        'compare'		=> '<=',
    	        'value'			=> $date_now,
    	        'type'			=> 'DATETIME'
    	    ),
    	    array(
    	        'key'			=> 'end_date',
    	        'compare'		=> '>=',
    	        'value'			=> $date_now,
    	        'type'			=> 'DATETIME'
    	    )
        ),
    	'order'				=> 'ASC',
    	'orderby'			=> 'meta_value',
    	'meta_key'			=> 'start_date',
    	'meta_type'			=> 'DATE'
    ));
    
    if( $posts ): ?>
    
    	<h2>Events on right now</h2>
    	<ul id="events">
    		<?php foreach( $posts as $p ): ?>
    			<li>
    				<strong><?php echo $p->post_title; ?></strong>: <?php the_field('start_date', $p->ID); ?> -  <?php the_field('end_date', $p->ID); ?>
    			</li>	
    		<?php endforeach; ?>
    	</ul>
    
    <?php endif; ?>
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Filtering by date’ is closed to new replies.