Support

Account

Home Forums Front-end Issues Trying to sort posts by acf date ASC, but only if field is not empty.

Unread

Trying to sort posts by acf date ASC, but only if field is not empty.

  • I’m trying to achieve a simple post where the posts are programs, being sorted by date of program. This is no problem, however in the backend, they have the ability to not add a date, which displays a TBA on the front end. Everything I’m trying is displaying the posts that have a TBA, or empty date field first, then ASC. Here is my code, I’ve tried several iterations…

    <?php
    $today = date('Ymd');
    
    $paged = get_query_var('paged') ? get_query_var('paged') : 1; 
    $args = array (
    	'$paged'		 	=> 	$paged,
    	'post_type'			=> 	'programs',
    	'meta_query'		=> array(
    		'key'				=> 	'date',	
    		'compare'			=>	'>=',
    		'value'				=> '$today',
    		),
    	'meta_key'			=> 	'date',
    	'orderby' 			=>	'meta_value_num',
    	'order' 			=>	'ASC'
    	);
    $query = new WP_Query($args);
    	if ($query->have_posts()) :
    	while ($query->have_posts()) : $query->the_post(); ?>  

    I’m trying to say that the date needs to be equal or gt todays date to qualify. Not sure of best methods here…

Viewing 1 post (of 1 total)

The topic ‘Trying to sort posts by acf date ASC, but only if field is not empty.’ is closed to new replies.