Title: order by datepicker value
Last modified: August 30, 2016

---

# order by datepicker value

 *  [edumusa](https://wordpress.org/support/users/edumusa/)
 * (@edumusa)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/order-by-datepicker-value/)
 * hello….
 * I need some help with advanced custom fields datepicker and yearly archives.
   
   I’m no programmer, so i do some googling to achieve my goals.
 * i got a code from [this link](http://support.advancedcustomfields.com/forums/topic/past-events-page-display-posts-by-datepicker-year/)
   that displays my custom posts grouped by year.
 * Works almost well…
 * Posts, as well as the years itself, are displaying ordered by the most recent
   published item, but i want them to be ordered by the datepicker custom field 
   value.
 * So I get a post list as:
 * Events from 2014
    • Event 2 from 2014 • Event 1 from 2014 • Event 3 from 2014
 * Events from 2015
    • Event 2 from 2015 • Event 1 from 2015
 * Events from 2010
    • Event 2 from 2010 • Event 1 from 2010 • Event 4 from 2010•
   Event 3 from 2010
 * Events from 2012
    • Event 1 from 2012 • Event 3 from 2012 • Event 2 from 2012
 * when i actually want them to display correctly as:
 * Events from 2015
    • Event 2 from 2015 • Event 1 from 2015
 * Events from 2014
    • Event 3 from 2014 • Event 2 from 2014 • Event 1 from 2014
 * Events from 2012
    • Event 3 from 2012 • Event 2 from 2012 • Event 1 from 2012
 * Events from 2010
    • Event 4 from 2010 • Event 3 from 2010 • Event 2 from 2010•
   Event 1 from 2010
 * my code so far:
 *     ```
       $years = array();
       		if( $posts ) {
       			foreach( $posts as $post ) 	{
       				setup_postdata( $post );
   
       				// get dateyear
       				$date = date_create( get_field('inicio') );
       				$year = date_format($date,'Y');
   
       				// create new year if not already exists
       				if( !isset( $years[ $year ]) ) 	{
       					$years[ $year ] = array(
       					'title' 	=> $year,
       					'orderby' => $year,
       					'order' => 'DESC'
       					);
       				}
   
       				// add post to year
       				$years[ $year ]['exposicoes'][] = $post;
   
       			}
       			wp_reset_postdata();
       		}
   
       		if( $years ) {
       			foreach( $years as $year ) {
   
       				echo '<h2 style="background:#DDD; margin-bottom:32px;"><a href="';
       				echo $year['title'];
       				echo '">';
       				echo $year['title'];
       				echo '</a></h2>';	
   
       				if( $year['exposicoes'] ) {
       					foreach( $year['exposicoes'] as $post ) {
       						setup_postdata( $post );
   
       						echo '<h3><a href="';
       						the_permalink();
       						echo '">';
       						echo the_field('inicio');
       						get_template_part( 'content', 'expos' );
       						echo '</a></h3>';
   
       					}
       				}
       			}
   
       			wp_reset_postdata();
       		}
       ```
   
 * what am i missing?
    any help please!!
 * [https://wordpress.org/plugins/advanced-custom-fields/](https://wordpress.org/plugins/advanced-custom-fields/)

Viewing 1 replies (of 1 total)

 *  [Matthew Ray](https://wordpress.org/support/users/rayman813/)
 * (@rayman813)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/order-by-datepicker-value/#post-6625578)
 * Try doing a `ksort` before your loop:
 *     ```
       ksort( $year['exposicoes'] );
       foreach( $year['exposicoes'] as $post ) { ...
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘order by datepicker value’ is closed to new replies.

 * ![](https://ps.w.org/advanced-custom-fields/assets/icon.svg?rev=3207824)
 * [Advanced Custom Fields (ACF®)](https://wordpress.org/plugins/advanced-custom-fields/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/advanced-custom-fields/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/advanced-custom-fields/)
 * [Active Topics](https://wordpress.org/support/plugin/advanced-custom-fields/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/advanced-custom-fields/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/advanced-custom-fields/reviews/)

## Tags

 * [datepicker](https://wordpress.org/support/topic-tag/datepicker/)
 * [orderby](https://wordpress.org/support/topic-tag/orderby/)
 * [sort](https://wordpress.org/support/topic-tag/sort/)

 * 1 reply
 * 2 participants
 * Last reply from: [Matthew Ray](https://wordpress.org/support/users/rayman813/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/order-by-datepicker-value/#post-6625578)
 * Status: not resolved