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, 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>
Was it helpful?

Solution

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,
);
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top