Support » Plugin: Advanced Custom Fields » 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 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/

Viewing 1 replies (of 1 total)
  • 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.