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

I did an ajax filter to filter posts per years, however the filter isn’t showing all items. Years are displayed in DESC order, but not all years appear 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>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

LOL– I had to just add posts_per_page in my $terms_year query. Leaving this up in case people need it…

$terms_year = array(
'post_type'         => array('publications'),
'posts_per_page'=>-1,
);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x