Support

Account

Home Forums General Issues Sorting by custom fields

Solving

Sorting by custom fields

  • I have followed this guide http://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/ when trying to sort my WooCommerce products on which I have added a custom field called “Release Date” which I have configured as follows:

    Field Type: Date Picker
    Save Format: dd/mm/yy
    Display Format: dd/mm/yy

    When I try to sort on this field though, it sorts alphanumerically, treating the value as a strong (instead of a date)

    How do I get the WP to sort as a date instead of a string?

  • what is your meta query? You should not be using the type of ‘DATE’ with an ACF date field. Use the default ‘CHAR’ type as shown on this page http://www.advancedcustomfields.com/resources/date-picker/ under Query posts based on date

  • Thanks for your response.
    Here is the filter I am using, (I am using it with WooCommerce products) and I am not using type of ‘DATE’ (although I did try when I couldn’t get the following to work)

    	$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    	switch( $orderby_value ) {
    	
    		case 'release_date':
    			$sort_args['orderby']  = 'meta_value';
    			$sort_args['order']    = 'desc';
    			$sort_args['meta_key'] = 'release_date';
    			break;
    		
    	}
    	
    	return $sort_args;
  • Try changing ‘desc’ to ‘DESC’

  • the problem still persists.

    It is just sorting on the first number i.e. all 31st of any month and year and listed first, 30th of any month and year are listed next…

  • ACF stores dates as ‘YYYMMDD’ Sorting on this field as either a number or as text should work.

    couple of questions

    You said that this is a date field, but is this date field a sub field of a repeater or in a flexible content field?

    Can you successfully make this filter sort by anything else? For example do these change the order?

    'orderby' => 'title',
    'order' => 'ASC'
    'orderby' => 'title',
    'order' => 'DESC'
  • ACF stores dates as ‘YYYMMDD’ Sorting on this field as either a number or as text should work.

    Even though, as I posted in the original post, I set it as:

    Save Format: dd/mm/yy

    Does it ignore this setting?

    Yes, it does sort by title fine – but I need it to do release date – as I said it is sorting, I know this, but it is not sorting CORRECTLY – it is sorting it alphanumerically instead of chronologically.

    I am at a point where i think i will change the data to Save Format YYYYMMDD so that it will sort alphanumerically and win/win but I wanted to figure out if it is a bug or not.

  • I missed the part where you mentioned Save Format, but that also means you’re using ACF4. Sorry for the oversite.

    If you want to be able to sort by this field then you need to set the save format to something that can be sorted. To sort by a date in WP the format must be YYYY-MM-DD. to sort by but number or text the saved value still needs to be similar. Year, Month, Day.

    This is likely one of the reasons why the save format was removed from ACF5.

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

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