Hello
I am aiming to create a list of projects by year, like so
2018
project title 1
project title 2
2017
project title 1
project title 2
etc
I have a number field for the year (ex_year) and a title field (ex_title), I would like to show the year, and all projects that fall under that year.
this is how I list my projects so far, with no year sorting:
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'exhibition',
'meta_key' => 'ex_title',
'meta_value' => ''
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<div class="exie">
<a href="<?php the_permalink(); ?>">
<?php the_field('ex_title'); ?> </>
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<br>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I have a solution now
this is the code if anyone needs it:
<?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 );
?>
<!-- PAST EXHIBITIONS -->
<?php while (have_posts()) : the_post(); ?>
<?php
$date = get_field('ex_start');
$past_year = DateTime::createFromFormat('Ymd', $date);
$year = $past_year->format('Y');
if ($year !== $today) { ?>
<h2><?php echo $year; ?></h2>
<?php } ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php $today = $year; ?>
<?php
echo "<p><br /></p>";
?>
<?php endwhile; ?>
<?php endif; ?>
The topic ‘list by year’ is closed to new replies.
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.