Jump to content

I want to display years in descending order instead on 1,2,3...


Recommended Posts

Hi all,

Quick question as I haven't found anything from my Googling. 

I have a blogs on the site which utilizes pagination and I want to display years in descending order(i.e.: 2022, 2021,2020,.....) instead of 1,2,3...  

I'm not sure how to go about this so any direction would be helpful.




Link to comment
Share on other sites

I don’t think ProcessWire has this built in. Do you have gaps that you need to skip or maybe show as inactive? If not you can just find the oldest and newest page and iterate between them. Otherwise I suggest custom SQL to efficiently group by something like year(pages.published). I think RockFinder can also group things, might want to check that out.

Link to comment
Share on other sites

Grouping your posts by year is a different use case than pagination which is just showing a fixed number of posts per page. For example you might have a year with dozens of posts that still need to be paginated.

When we've needed to do this in the past we've used URL segments to pull out a year and then build a selector from that. We can then use the results from that query for pagination if need be.

The code below uses a URL like /blog/by-year/2020 ; you could easily build a subnav that has the years in you need to filter by.
Obviously you'd need to update the code to match your fields but hopefully it will point you in the right direction.

<?php namespace ProcessWire; 


// Do we have url parameters? 
if ($input->urlSegment1 ) {

 	// in this example we only filter by year
    if($input->urlSegment1 =='by-year'){

        $year_end=$year . '-12-31';
        $year_start=$year . '-01-01';

        if($year > 2000 && $year < 2050){ // not really santizing but I just don't want to encourage any Widdecombe of the Week behaviour.

        $results = $pages->find("template=news_item, publish_from<=".$year_end.",publish_from>=".$year_start.",limit=12, sort=publish_from");


        $filter_title="Sorry - unknown filter.";


    $results = $pages->find("template=news_item, limit=12, sort=-publish_from");

    echo '<h2>' . $filter_title .'</h2>';

if($results && $results->count){

    $pagination = $results->renderPager();

    echo '<div class="news-list">';

    foreach($results as $result) {
        echo '<div class="news-item">';
        echo '<div class="news-title"><a href="'.$result->url . '">' . $result->title .'</a></div>';
        echo '<div class="news-date">' . $result->publish_from .'</div>';
        echo '<div class="news-summary">' . $result->summary .'</div>';
        echo '</div>';

    echo "</div>";

    echo '<div class="text-center">' . $pagination .'</div>';

    echo '<div class="news-list">No results found.</div>';


  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...