Help With A Reverse Pagination Plugin

I saw this presentation about why pagination on the Internet is weird. https://speakerdeck.com/u/espylaub/p/pagination-on-the-internet-and-why-its-weird
It inspired me to try and make a WordPress plugin to do just that. Here’s what I’ve got so far…

<?php
/*
Plugin Name: Reverse Pagination
Description: Pagination starts higher and goes lower for more consistent archive page numbering.
Author: Russell Heimlich
Version: 0.1
*/ 


function reverse_pagination_pre_get_posts($t) {  
     $q = $t->query_vars;
     if ( empty($q['order']) || ((strtoupper($q['order']) != 'DESC') && (strtoupper($q['order']) != 'ASC')) ) {
        $q['order'] = 'ASC';
     }

     $t->query_vars = $q;
}

function reverse_pagination_wp() {
    global $wp_query;
    $wp_query->posts = array_reverse($wp_query->posts);
}

if( !is_admin() ) {
    add_action('pre_get_posts', 'reverse_pagination_pre_get_posts');
    add_action('wp', 'reverse_pagination_wp');
} 

I hook into the pre_get_posts action and switch the order query var to ASC instead of the default DESC.

Read More

Next I needed to reverse the order of the posts returned so they are in reverse chronological order. I chose the wp action for the reasons outlined in this answer https://wordpress.stackexchange.com/a/22768

So things are looking good so far. I just need to fix the first page. Right now if my blog has 50 pages and there is no paged parameter set the blog index page will show my very first post instead of the latest post. I need some logic to check if paged isn’t set to set it to the same value of $wp_query->max_num_pages for that query.

I also need to disable the canonical redirect that removes /page/1/ from the URL and tweak the get_pagenum_link() function so it returns /page/1/ for get_pagenum_link(1) http://hitchhackerguide.com/2011/02/12/get_pagenum_link/

If there is anything else that can be added to make this plugin better please speak up! Thanks and I hope we can make this happen by working together on it.

Related posts

Leave a Reply

1 comment

  1. This function will give you a url for a page number without stripping the url for page 1:

    function get_pagenum_link_full($pagenum = 1, $escape = true ) {
        global $wp_rewrite;
        $pagenum = (int) $pagenum;
        $request = remove_query_arg( 'paged' );
        $home_root = parse_url(home_url());
        $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
        $home_root = preg_quote( $home_root, '|' );
        $request = preg_replace('|^'. $home_root . '|i', '', $request);
        $request = preg_replace('|^/+|', '', $request);
        if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
            $base = trailingslashit( get_bloginfo( 'url' ) );
            $result = add_query_arg( 'paged', $pagenum, $base . $request );
        }
    }
    

    Modified from the original code for get_pagenum_link() as defined here http://phpxref.ftwr.co.uk/wordpress/wp-includes/link-template.php.source.html#l1408

    get_pagenum_link_full(1); will give you something like http://www.url.com/category/page/1 depending on your permalink settings.