Support

Account

Home Forums ACF PRO list by year

Helping

list by year

  • 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; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.