Support

Account

Home Forums General Issues Filtering by date Reply To: Filtering by date

  • 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; ?>