0

Let's say I have 10 posts on my blog with a posts per page limit of 7, this is what I get:

  • Page 1(home): 7 posts
  • Page 2: 3 posts

But I need this:

  • Page 1(home): 3 posts
  • Page 2: 7 posts

I need to start posts from the last page, any idea on how to do it?

0

If you don't mind the cost of calling COUNT to first get the total number of posts (you have a dynamic first page in terms of numbers of posts so it doesn't work with a simple offset) here is something that I've wrote (it's late and I am tired so it might have some bugs / might not be the right solution):

// This code can be placed in the 'functions.php' file.
function wt_posts_clauses_hook( $clauses, $query ) {
    if ( $query->is_main_query() && $query->is_home() ) {
        global $wpdb;

        extract( $clauses );

        if ( ! empty( $groupby ) ) $groupby = 'GROUP BY ' . $groupby;

        $sql = "SELECT COUNT(ID) FROM $wpdb->posts $join WHERE 1=1 $where $groupby";
        $no_posts = $wpdb->get_var( $sql );
        $posts_per_page = get_option( 'posts_per_page' );
        $extra = $no_posts % $posts_per_page;
        $paged = absint( $query->get( 'paged' ) );

        if ( ! $paged ) $paged = 1;

        $from = ( $paged - 1 ) * $posts_per_page;
        $to = $posts_per_page;

        if ( $extra ) {
            if ( $paged < 2 ) {
                $from = 0;
                $to = $extra;
            } else {
                $from = $posts_per_page * ( $paged - 2 ) + $extra;
            }
        }           

        $clauses['limits'] = "LIMIT {$from}, {$to}";
    }

    return $clauses;
}
add_filter( 'posts_clauses_request', 'wt_posts_clauses_hook', 99, 2 );

You can probably implement a caching mechanism if you don't want to run this on every listing page call by reading the found_posts property. If it doesn't correspond to the cache entry you simply delete the cache. Of course, you first have to modify this function above to initially try and read the $no_posts value from cache. If there is none then run the $sql query and create the cache with the corresponding $no_posts value.

  • Excuse my ignorance, I'm a total newbie to WP, where exacly should I put this piece of code? Inside which file? – Xriuk Aug 11 '15 at 10:02
  • This would go in the functions.php file. – webtoure Aug 11 '15 at 10:36
  • So, I put this code inside my functions.php and my site crashes... – Xriuk Aug 11 '15 at 16:20
  • As of now, I have tested this code on the following WordPress themes: Twenty Fifteen, Twenty Fourteen and Twenty Thirteen. The code requires PHP version >= 5.3.0 which was launched almost 6 years ago. Perhaps that is the issue. I will update the code and remove the closure, hopefully this will fix your issue. – webtoure Aug 11 '15 at 16:33
  • Just one pro tip, never ever use extract(). It was removed from core and the codex for very specific reasons. It is unreliable and almost impossible to debug – Pieter Goosen Aug 12 '15 at 7:21

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.