Support

Account

Home Forums General Issues Using ACF date to sort custom post types not sorting correctly

Solved

Using ACF date to sort custom post types not sorting correctly

  • Hi,
    I was having an issue getting my loop to work sorting by the ACF date. It was because I was using the date picker that includes the time, then striping the time in PHP. Switching to the date picker with only the date solved my issue.

    <?php $event_page_Query = new WP_Query(
        array( 
          'posts_per_page' => -1,
          'post_type' => 'newton_events',
          'orderby' => 'meta_value_num',
          'meta_key'  => 'event_date_time',
          'order' => 'ASC',
        )
      ); ?>
    
      <?php while ( $event_page_Query->have_posts() ) : $event_page_Query->the_post(); ?>
    
        <?php if ( get_field('event_date_time') ) : ?>
          <p><?php the_field('event_date_time'); ?></p>
        <?php endif; ?>
    
      <?php endwhile; ?>
    
      <?php wp_reset_postdata(); ?>

    I would delete this post! But there is no delete button, only Edit!

  • We’ll leave it here because someone else might have a similar question.

  • I got:

    $args = array(
    	'post_type' => 'foto_archief',
    	'orderby' => 'meta_value_num',
    	'meta_key'  => 'date',
    	'order' => 'ASC',
    	'posts_per_page' => -1
    );

    but it only filters on month so I got first jan than feb ect… but the years and dates are all over the place.

    My input date is 10/20/2016 and my output date is 24 januari, 2011

  • What version of ACF are you using? If you are using ACF4 it allows you to set the storage format. You can only sort by dates when the storage format is YYYYMMDD or YYYY-MM-DD or anything where the order is Year-Month-Day.

    ACF5 removes the ability to set the storage format, so I’m assuming you’re using 4 because this should not be an issue with 5.

  • I am using Version: 5.4.8

  • and it’s a date field?

  • This is my field (sorry in Dutch):

    This is the loop

    
    <?php $args = array(
    	'post_type' => 'foto_archief',
    	'orderby' => 'meta_value_num',
    	'meta_key'  => 'date',
    	'order' => 'ASC',
    	'posts_per_page' => -1
    );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) :
    	while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    		<h3><?php the_title(); ?></h3>
    		<p><?php echo get_field('date'); ?></p>
    
    <?php endwhile; endif; wp_reset_postdata(); ?>
    

    And this is what the list looks like

    1. 26 januari, 2012
    2. 27 januari, 2012
    3. 24 januari, 2011
    4. 24 januari, 2011
    5. 24 januari, 2011
    6. 26 januari, 2012
    7. 15 december, 2013
    8. 22 december, 2016
    9. 27 september, 2016
  • There’s no reason, looking at just what you’ve provided, whey the posts should not be returned in the correct order.

    There are 2 possible causes that I know of.

    1) There is another field for the same post type with the same field name “date”. Look for other fields on the same post type with the same name.

    2) There is a filter somewhere that’s interfering with the query. Try disabling plugins and changing themes.

  • Could it be that date is already used by the default wordpress date and and naming the field completely differed like photo_archive_date would fix the problem?

    If so could you rename the field and have all the content in the field update with it?

  • The WP date field is not a post meta field. The only think that could interfere is another meta field named “date”

    It is almost always better to use names that you know will be unique.

    There isn’t any way that currently exists that will let you automatically update all the fields if you change the field name. This would be additionally problematic in this case if there is another field using the same name since you wouldn’t know what one to change in the database.

  • @hube2 sorry for the late reply, but you know busy and stuff and the client was ok with the filtering being based on the month for now.

    But I now have revisited the issue and renamed the field date to photo_date of which I am sure there is no duplicate of, but the issue still persists

    a copy of the data I use can be found here:
    https://docs.google.com/spreadsheets/d/1t3LD1z83DsHgA_SzvYxLk_4dzrrtYd0iRyx7sK7GzWk/edit#gid=0

    And if you want to look at the data being in use check it out here”
    http://www.pletterij.nl/foto-archief/

    I have no idea why it’s happening, but maybe you see something obvious.

  • Are you importing from that spreadsheet? Dates need to be stored YYYYMMDD in ACF 5. Also, when you are sorting by date they need to be in a similar format, with year first, then month, then day. If the stored dates are in any other order then they will not sort properly.

  • @hube2 well, that solved the issue. Thanks.

  • I hate to revive this, but it’s one of the more relevant threads I could find.

    I just created a CPT with some ACF custom fields in it to replace an old plugin that we’re ditching. All works well, but I cannot get the sorting to appear correctly.

    My data is stored in ACF date-picker “format” for new entries I assume, but the old entries did come in via a CSV import of heavily-massaged data (old plugin wanted $90 for an “export” feature, so F-that). Can I assume the dates are stored correctly if they are displaying correctly when pulling them via get_field()? Or no? This is ACF5, just noting that as there was mention of the format changing between 4 and 5.

  • The way the date was stored in version 5 as “Ymd” format or “YYYYMMDD”. In 4, you were allowed to set the storage format, so it may or may not have been stored that way depending on how the field was set up. During the update from ACF4 to 5 I think that the values in the DB were updated.

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

The topic ‘Using ACF date to sort custom post types not sorting correctly’ is closed to new replies.