Show and paginate only child categories with wordpress query I'm using this piece of code to show and paginate only child categories but this is horribly slow (49 queries in 9,799 seconds.).

Where did I make mistakes and how can I make this run faster?

&lt;?php

$current_cat = get_query_var('cat');
$attributes = array('terms_per_page' =&gt; 20, 'taxonomy' =&gt; 'category');

// Sanitize and validate our inputs and set variables
$tpp = filter_var( $attributes['terms_per_page'], FILTER_VALIDATE_INT );
$taxonomy = filter_var( $attributes['taxonomy'], FILTER_SANITIZE_STRING );

// Make sure our taxonomy exists to avoid unnecessary work
if ( !taxonomy_exists( $taxonomy ) )
return false;

// Our taxonomy exists, lets continue
// Get the term count to calculate pagination.
$term_count = count( get_terms('category',array('hide_empty'=&gt;'1','child_of' =&gt; $current_cat) ) );

// We have terms, now calculate pagination
$max_num_pages = ceil( $term_count / $tpp );
// Get current page number. Take static front pages into account as well
if ( get_query_var('paged')){
$paged = get_query_var('paged');
} elseif ( get_query_var('page')) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
// Calculate term offset
$offset = ( ( $paged - 1 ) * $tpp );

// We can now get our terms and paginate it
$args = array(
'number'     =&gt; $tpp, // Amount of terms to return
'offset'     =&gt; $offset, // The amount to offset the list by for pagination
'child_of'   =&gt; $current_cat,
'orderby'    =&gt; 'name', 
'order'      =&gt; 'ASC', 
'hide_empty' =&gt; '1',
);
// Set our variable to hold our string
$output = '';
$wpbtags = get_terms( $taxonomy, $args );

if ( ! empty($wpbtags)) {
foreach ($wpbtags as $category) { 

//first get the current category ID
$cat_prefix = "category_";
$cat_id = $category-&gt;term_id;
//then i get the data from the database
$join_cat = $cat_prefix . $cat_id;
$cat_data = get_option($join_cat);?&gt;

&lt;a href="&lt;?php echo get_category_link($category-&gt;term_id); ?&gt;"     onclick="ga('send', 'event', 'ClickNav', 'cats', 'Cat - &lt;?php echo $category-    &gt;cat_name; ?&gt;');"&gt;
&lt;div class="one_fourth&lt;?php echo ( ++$counter == 4 )?' last_column':''; ?&gt; home-colour" title="&lt;?php echo $category-&gt;name; ?&gt;"&gt;

&lt;?php if (!empty($cat_data['cat_img_url'])) : ?&gt;
&lt;h2 style="text-align:center"&gt; &lt;?php echo $category-&gt;name; ?&gt; &lt;/h2&gt;
&lt;img class="res_img" src="&lt;?php echo $cat_data['cat_img_url'] ;?&gt;" alt="&lt;?php echo $category-&gt;cat_name; ?&gt;" width="310" height="209" /&gt;

&lt;?php elseif (empty($cat_data['cat_img_url'])) :?&gt;
&lt;h2 style="text-align:center"&gt; &lt;?php echo $category-&gt;name; ?&gt; &lt;/h2&gt;
&lt;img class="res_img" src="&lt;?php echo get_stylesheet_directory_uri(); ?&gt;/images/subcat-img-na.png" alt="Posts - &lt;?php echo $category-&gt;cat_name; ?&gt;"/&gt;

&lt;?php else : ?&gt;
&lt;?php endif; ?&gt;
&lt;/div&gt;&lt;!-- eof cat div --&gt; &lt;/a&gt;

&lt;?php } ?&gt;   

&lt;?php /* COUNTS FOUR ADDS LAST COLUMN */ if ( $counter == 4 ) : ?&gt;
&lt;div class='clear'&gt; &lt;/div&gt;
&lt;?php $counter = 0; endif; wp_reset_query(); ?&gt;

&lt;div class='clear'&gt; &lt;/div&gt;

&lt;?php echo '&lt;div class="wp-pagenavi" style="text-align:center"&gt;';
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' =&gt; str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' =&gt; '?paged=%#%',
'current' =&gt; max( 1, get_query_var('paged') ),
'total' =&gt; $max_num_pages = (ceil( $term_count / $tpp))
) );

echo '&lt;/div&gt;';
?&gt;   

&lt;?php wp_reset_postdata(); ?&gt;

&lt;?php  /* IF THERE'S NOTHING TO SHOW */ } else {?&gt;

// Show something if there is no posts here

&lt;?php } ?&gt;
The real problem here is that you are querying in loops (almost always a bad pattern) because you are relying on built-in Wordpress querying functions, rather than writing raw SQL to query all this data at once.

My guess is you can do a single query to get both the paginated results and the total number of rows without limit being applied (by using SQL_CALC_FOUND_ROWS).

You might consider opening StackOverflow question specifically about the multiple/slow query problems along with information on your database tables relevant to these queries to get a suggestion on how to write an appropriate query.



Outside of that, I find your code incredibly difficult to read, and while some of that might have to do with following style conventions in Wordpress (which really bad IMO), you should at least indent you code and place comments properly (i.e. not prepending comment on code lines).