Too many posts cause slow load when paginating

I have about 10k posts and I made a page that lists all posts but it is paginated every 54posts. It gives 131 queries in 6.730 seconds which is pretty slow to load. Is there a way to make it load faster?

This is my functions page

<?php
/* Custom code goes below this line. */

add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
add_filter( 'use_default_gallery_style', '__return_false' );

/* Add the Javascript */
$path = get_stylesheet_directory_uri() .'/js/';
wp_enqueue_script('post-navigation', $path.'jquery.navigate.js', array('jquery'));

function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();


$transformed_content = apply_filters('the_content',$post->post_content);


$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $transformed_content, $matches);
$first_img = $matches [1] [0];

if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}

function pagination($pages="", $range = 4)
{  
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href="".get_pagenum_link(1)."">&laquo; First</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href="".get_pagenum_link($paged - 1)."">&lsaquo; Previous</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href="".get_pagenum_link($i)."" class=\"inactive\">".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href="".get_pagenum_link($pages)."">Last &raquo;</a>";
         echo "</div>\n";
     }
}

// This function should go in functions.php
function mam_posts_where ($where) {
   global $mam_global_where;
   if ($mam_global_where) $where .= " $mam_global_where";
   return $where;
}
add_filter('posts_where','mam_posts_where');

/* Custom code goes above this line. */
?>

And this is my page template

<?php
/*
Template Name: List
*/
?>

<?php get_header(); ?>

        <div id="container">
            <div id="content2" role="main">

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post', 'posts_per_page' => 54, 'paged' => $paged );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<div class="seriesbox">
<a class="size-thumbnail" href="https://wordpress.stackexchange.com/questions/76055/<?php the_permalink(); ?>"><img src="<?php echo catch_that_image() ?>"/></a>
<div class="seriestitle"><h2><?php the_title() ?></h2></div>
</div>
<?php endwhile; ?>

<!-- then the pagination links -->
<?php if (function_exists("pagination")) {
    pagination($additional_loop->max_num_pages);
} ?>

            </div><!-- #content -->
        </div><!-- #container -->

        <?php get_footer(); ?>

1 Answer
1

Yes, use caching. But even with caching you should rethink what you are actually doing because the cache for page/2 should change every time you publish a new post which might make caching pointless depending on the amount of traffic to that page.
You should probably rethink what you are trying to achieve there, why 54 and not less? 54 images on one page is a lot and the page will be slow not only because of the HTML generation but also because fetching the images will take time.

Leave a Comment