Support

Account

Home Forums Front-end Issues Sorting by date

Solved

Sorting by date

  • I have a date picker for my CPT and would like sort the posts by that date. Unfortunately, everything I’ve tried does not seem to work, even when following documentation: http://www.advancedcustomfields.com/resources/field-types/date-picker/

    Here’s my query:

    	$args = array(
    		'post_type' => 'portfolio',
    		'posts_per_page' => -1,
    		'meta_key' => 'date_received',
    		'orderby' => 'meta_value_num',
    		'order' => 'DESC'
    	);
    	$query = new WP_query($args);
    		
    	$output = '<ul class="awards">';	
    	
    	while ($query->have_posts()) { $query->the_post();			
    			
    		$award_name = get_field('award');
    		$date = get_field('date_received');
    			$y = substr($date, 0, 4);
    			$m = substr($date, 4, 2);
    			$d = substr($date, 6, 2);
    			$time = strtotime("{$d}-{$m}-{$y}");
    			
    		if($award_name) {				
    			$output .= '<li><div class="date">';
    			$output .= date('F Y', $time);
    			$output .= '</div><h4>';
    			$output .= get_field('award');
    			$output .= '</h4><div class="project-title">';
    			$output .= get_the_title();
    			$output .= '</div></li>';
    		}
    		
    	}
    	
    	$output .= '</ul>';
    	
    	// reset the query
    	wp_reset_postdata();
    	
    	// return something
    	return $output;

    The date picker field has all the default settings.

  • Hi @NWTD,

    Your code here never prints anything? I’m guessing you want to print $output to screen? In that case you must echo it, and it is not necessary to return anything.

    If this is part of a function, are you sure youre printing the result on the page?

    Cheers

  • My apologies for the confusion.

    My code prints out the loop that I want, however, it is not sorted by the date field that I specified. I just need my loo[p to list items in chronological order based on the date field that is specified in the query.

    'meta_key' => 'date_received',
    'orderby' => 'meta_value_num',
  • Hi @NWTD

    Can you confirm that all posts have saves the date picker value with the save_format setting of ‘yymmdd’?

    Thanks
    E

  • Hi @NWTD

    Thanks for the screenshot. Can you confirm that this setting has never been changed and that all posts have saved in the correct format?

    Perhaps you have another issue in your WP_Query args. The best way to find the issue is to remove parts from the args one at a time until the issue resolves itself.

    
    $args = array(
    		'post_type' => 'portfolio',
    		'posts_per_page' => -1,
    		'meta_key' => 'date_received',
    		'orderby' => 'meta_value_num',
    		'order' => 'DESC'
    	);
    	$query = new WP_query($args);
    		
    	var_dump( $query->have_posts() );
    

    Perhaps remove the orderby and order? perhaps remove the meta_key also?

    Thanks
    E

  • Thanks for the response @elliot.

    My apologies for not clearing this up sooner. My query displays the posts. They’re just not ordered by the meta_key defined in the query. The meta_key is a datepicker from ACF.

    Here’s how the query looks on the front end: http://imgur.com/ANmN8ft

    Notice how the dates are not in order? The posts are in order by the post date, not the date set forth by the advanced custom field.

    I hope that helps to clear up any confusion.

  • Hi @NWTD

    Try removing ACF from the equation by changing your ‘orderby’ value to ‘title’. Do the posts order by title? Perhaps also try ‘rand’.

    If these work, perhaps try 'orderby' => 'meta_value' (without the ‘_num’)

    Also, check that you don’t have any ‘pre_get_post’ filters that are modifying the args

  • Ah!

    Also, check that you don’t have any ‘pre_get_post’ filters that are modifying the args

    That put me int he right direction. none of the other orderby arguments seem to work.

    I changed up the query to:

    $args = array(
    	'post_type' => 'portfolio',
    	'posts_per_page' => '-1',
    	'meta_key' => 'date_received',
    	'orderby' => 'meta_value_num',
    	'order' => 'DESC'
    		
    );
    remove_all_filters('posts_orderby');
    $query = new WP_query($args);

    And that fixed it. If this was in the documentation, my apologies for not seeing it. Either way, I’m good to go and thank you for your help.

  • Great support. Same problem, and the solution ‘remove_all_filters()’ did the trick. Thanks, both of you!

Viewing 10 posts - 1 through 10 (of 10 total)

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