0

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>
0

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,
);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.