Support

Account

Home Forums Front-end Issues Sorting by date

Solved

Sorting by date

    • NWTD

    • January 31, 2014 at 4:26 am

    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.

    • James

    • January 31, 2014 at 12:53 pm

    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

    • NWTD

    • February 1, 2014 at 3:23 am

    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',
    • Elliot

    • February 2, 2014 at 12:55 pm

    Hi @nwtd

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

    Thanks
    E

    • NWTD

    • February 4, 2014 at 3:01 am

    Yes it is. See: http://imgur.com/d3XV93C

    • Elliot

    • February 4, 2014 at 9:44 am

    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

    • NWTD

    • February 4, 2014 at 9:59 am

    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.

    • Elliot

    • February 4, 2014 at 10:21 am

    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

    • NWTD

    • February 4, 2014 at 10:33 am

    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.

    • ggeiger

    • February 5, 2014 at 4:36 am

    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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.