Trying to list years (for a filter) but not all years display

Question

I did an ajax filter to filter posts per years, however the filter isn’t showing all items. Years are displayed in DESC order, and they stopped displaying after 2012. I don’t have any items in 2011 – which makes me think something is breaking when it gets into a year with no items? But I don’t understand….

<?php $terms_year = array(
    'post_type'         => array('publications'),
);

$years = array();
$query_year = new WP_Query( $terms_year );

if ( $query_year->have_posts() ) :
    while ( $query_year->have_posts() ) : $query_year->the_post();
        $year = get_the_date('Y');
        if(!in_array($year, $years)){
            $years[] = $year;
        }
    endwhile;
    wp_reset_postdata();
endif;
?>

To show the filter:

<ul class="d-flex js-filter px-0 pb-3 justify-content-center">
<span class="text-uppercase font-weight-bold"><?php _e( 'Year:', TEXTDOMAIN ); ?></span>
<li class="text-center js-filter-item"><a class=""  data-allr="all"><?php _e( 'All', TEXTDOMAIN ); ?></a></li>
<?php foreach( $years as $year ) {?>
<li class="text-center js-filter-item">
<a class=""  data-year="<?php echo $year;?>" ref="#"><?php echo $year;?></a>
</li>
<?php }?>
</ul>
0
Cynthia Lara 4 months 0 Answers 15 views 0

Leave an answer