Home › Forums › General Issues › Past Events Page, Display Posts by DatePicker Year › Reply To: Past Events Page, Display Posts by DatePicker Year
@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
}
?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.