Support

Account

Home Forums General Issues Past Events Page, Display Posts by DatePicker Year

Solved

Past Events Page, Display Posts by DatePicker Year

  • I have this code working to display EVENTS (custom post type posts) listed by DatePicker field date, descending. I’m wondering if it’s possible to list the posts with subheading dividers for year?, ie:

    Events from 2013

    • Link to [Event 1 from 2013] Post
    • Link to [Event 2 from 2013] Post
    • Link to [Event 3 from 2013] Post

    Events from 2012

    • Link to [Event 1 from 2012] Post
    • Link to [Event 2 from 2012] Post
    • Link to [Event 3 from 2012] Post

    Events from 2011

    • Link to [Event 1 from 2011] Post
    • Link to [Event 2 from 2011] Post
    • Link to [Event 3 from 2011] Post
    <?php get_header(); ?>
    
    	<?php 
    	/* Order Posts based on Date Picker value */
    	$posts = get_posts(array(
    		'post_type' => 'event', // name of custom post type
    		'meta_key' => 'enddate', // name of custom field
    		'orderby' => 'meta_value_num',
    		'order' => 'DESC'
    	));
    	 
    	if( $posts )
    	{
    		foreach( $posts as $post )
    		{
    			setup_postdata( $post );
    
    				echo '<h3><a href="';
    				the_permalink();
    				echo '">';
    				the_title();
    				echo '</a></h3>';
    
    				$date = date_create(''.get_field('enddate').'');
    				echo date_format($date,'F d, Y');
    
    		}
    		wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    	}
    ?>
          
    <?php get_footer(); ?>
  • Hi @codeview

    You can achieve this by adding in some simple logic to sort the posts into another array which is divided by years like so:

    
    <?php 
    
    $years = array();
    
    if( $posts )
    {
    	foreach( $posts as $post )
    	{
    		setup_postdata( $post );
    		
    		// get date
    		$date = date_create( get_field('enddate') );
    		
    		
    		// get year
    		$year = date_format($date,'Y');
    		
    		
    		// create new year if not already exists
    		if( !isset( $years[ $year ]) )
    		{
    			$years[ $year ] = array(
    				'title' => 'Events from ' . $year,
    				'posts' => array()
    			);
    		}
    		
    		
    		// add post to year
    		$years[ $year ]['posts'][] = $post;
    		
    	}
    	wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    }
    
    if( $years )
    {
    	foreach( $years as $year )
    	{
    		echo '<h3>' . $year['title'] . '</h3>';
    		
    		if( $year['posts'] )
    		{
    			foreach( $year['posts'] as $post )
    			{
    				setup_postdata( $post );
    				
    				// get date
    				$date = date_create( get_field('enddate') );
    		
    				echo '<h3><a href="';
    				the_permalink();
    				echo '">';
    				the_title();
    				echo '</a></h3>';
    	
    				
    				echo date_format($date,'F d, Y');
    			}
    		}
    	}
    	
    	wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    }
    
     ?>
    
  • Great! Thank you, Elliot.

  • Hello, pardon me for returning to this old post, i have a similar need, and the code does work, but ordering is not. i’m trying to order by the datepicker value, but posts are ordering by the most recent publish, not the datepicker. I’m using custom post.
    For example, if i publish a new post for 2011 in the datepicker, the year 2011 goes to top, so i have something like: 2011… 2015…2013… 2010… 2014.
    Any clues?

  • Hi

    I am waking up this post again because I am looking for the same thing, but with custom posts.

    My post type is ‘exhibit’ and I have a date picker called ‘ex_start’

    I need help to adapt Elliots fine code to list my fields by year with the year as title

    thank you!

  • @edumusa my apologies for not responding all that time ago 🙁 Perhaps this will help you as well (though, I doubt you still need it)

    @fuberator – here is my code, using Custom Post Type “event”. It’s my original post code combined with Elliot’s answer (with a few changes, I think…it’s been awhile). I hope it helps:

    <?php              
    
                $currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
    
                    /* Order Posts based on Date Picker value */
                    $posts = get_posts(array(
                        'posts_per_page' => -1,
                        'post_type' => 'event', // name of custom post type
                        'meta_key' => 'event_enddate', // name of custom field
                        'orderby' => 'meta_value_num',
                        'order' => 'DESC',
                        'meta_query'=> array(
                            array(
                              'key' => 'event_enddate',
                              'compare' => '<',
                              'value' => $currentdate,
                              'type' => 'DATE',
                        ))
                    ));
    
                    $years = array();
    
                    if( $posts )
                    {
                        foreach( $posts as $post )
                        {
                            setup_postdata( $post );
                            
                            // get date
                            $date = date_create( get_field('event_enddate') );
                            
                            // get year
                            $year = date_format($date,'Y');
                            
                            // create new year if not already exists
                            if( !isset( $years[ $year ]) )
                            {
                                $years[ $year ] = array(
                                    'title' => $year,
                                    'posts' => array()
                                );
                            }
                            
                            // add post to year
                            $years[ $year ]['posts'][] = $post;
                            
                        }
                        wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
                    }
    
                    if( $years )
                    {
                        foreach( $years as $year )
                        {
                            echo '<h2>' . $year['title'] . '</h2>';
                            echo '<ul>';
                            if( $year['posts'] )
                            {
                                foreach( $year['posts'] as $post )
                                {
                                    setup_postdata( $post );
                                    
                                    // get date
                                    $date = date_create( get_field('event_enddate') );
                            
                                    echo '<li><a href="';
                                    the_permalink();
                                    echo '">';
                                    the_title();
                                    echo '</a><br>';
                                    echo date_format($date,'F d, Y');
                                    echo '</li>';
                                }
                            }
                            echo '</ul>';
                        }
                        
                        wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
                    }
             ?>
  • @codeview muchas gracias! i have my own thing going now and it works,

    I am now at the next step where i need to add a condition from a group switch to the mix (group/solo). I have the code, I just don’t know how to merge it in. Perhaps you have some advice.

    Most grateful in any case

    This is where I am at:

      <?php if (have_posts()) : ?>
        <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $today = date('Ymd');
    
        $past_args = array(
            'paged' => $paged,
            'posts_per_page' => -1,
            'post_type' => 'exhibition',
            'meta_query' => array (
                array (
                    'key' => 'ex_start',
                    'value' => $today,
                    'compare' => '<',
                    'type' => 'DATE'
                )
            ),
            'orderby' => 'meta_value_num',
            'order' => 'DESC'                   
        );
        query_posts( $past_args );
        ?> 
    
    <?php
    echo '<div class="biotitle">' . 'Solo Exhibitions' . '</div>';
    ?>
    
        <!-- PAST EXHIBITIONS -->
    <?php while (have_posts()) : the_post(); ?>
    
    <?php   
        $date = get_field('ex_start');
        $gallery = get_field('ex_gallery_name');
        $past_year = DateTime::createFromFormat('Ymd', $date);
        $year = $past_year->format('Y');
        
        // get start date
    $datum1 = get_field('ex_start', false, false);
    
    // make start date object
    $datum1 = new DateTime($datum1);
    
        // get end date
    $datum2= get_field('ex_end', false, false);
    
    // make end date object
    $datum2 = new DateTime($datum2);
    
        if ($year !== $today) { ?>
            <h2><?php echo $year; ?></h2><p><br /></p><p><br /></p>
    
        <?php } ?>
        <a href="<?php the_permalink(); ?>" class="exhibit"><?php the_title(); ?> , <?php echo $gallery; ?> , <?php echo $datum1->format('j M Y'); ?> - <?php echo $datum2->format('j M Y'); ?></a>
        <?php $today = $year; ?>
    
    <?php
    echo "<p><br /></p>";
    ?>
    
    <?php endwhile; ?>
    <?php endif; ?>
    
    

    and this is my final argument, that i am to inexperienced to merge in:

    <?php
    
    $posts = get_posts(array(
        'meta_query' => array(
            array(
                'key' => 'ex_groupsolo', // name of custom field
                'value' => 'solo',
            )
        )
    ));
    
    if( $posts ) {
        //...
    }
    
    ?>
  • Unfortunately I cannot help. Not much of a PHP expert myself 🙁 Glad I was able to help with the first part, though.

  • @codeview amazing what we copy paste coders can do these days 🙂

    have a good one

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

The topic ‘Past Events Page, Display Posts by DatePicker Year’ is closed to new replies.