I am trying to set up several archive pages (author.php and tag.php) that loop through a custom query. While I am able to utilize the instructions in the following link to create a custom query on a custom page template, I cannot seem to get pagination to work for me on a global template page.

How to fix pagination for custom loops?

For instance, I would like to be able to use the following link: site.com/author/author-name/ To pull up all of the custom post types called 'tasks' created by a specific author, utilizing author.php.


global $paged, $wp_query;
$qo = get_queried_object();
$author_id = $qo->ID;

$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' 
)): 1; 
$args = array( 
    'author'=> $author_id,
    'post_type'=> 'tasks', 
    'paged' => $paged

$the_query = new WP_Query( $args );  

// Pagination fix
$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $the_query;

if ( $the_query->have_posts() ) : 
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
endwhile;  endif; 

//OPTION 1 for pagination from Wordpress Codex sample
$big = 999999999; // need an unlikely integer

echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) 
) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );

// OPTION 2 Custom query loop pagination
// previous_posts_link( 'Older Posts' );
// next_posts_link( 'Newer Posts', $the_query->max_num_pages );


$wp_query = NULL;
$wp_query = $temp_query;


With option 1, the url that is set up for next links is: site.com/author/author-name/?paged=x Whereas, with option 2, the url that is set up for next links is: site.com/author/author-name/page/x

In both cases, when I click on the next page, I am directed to a 404 error. The key here is that I need the author name to be dynamic, so as I set up new users, I can pull up their custom posts using one of the above structures (preferrably site.com/author/author-name/page/x or even site.com/author-name/page/x).

Any insight would be greatly appreciated.

  • Basically, you can't paginate beyond the number of pages in the main query on any sort of archive. You need to alter the main query with pre_get_posts if you want to add custom post types to the query. – Milo Apr 25 '17 at 5:21
  • I'm not sure I understand your response. I was able to get the custom post types to the query successfully, the issue I have is that utilizing any of the common pagination functions like 'the_posts_pagination' on a template page like author.php or tag.php. Please see the following from the Wordpress Codex: Similarly, pre_get_posts will not work if used in template files (e.g., archive.php), since they are called after the query has been completed. – Ameer Bacchus Apr 25 '17 at 19:31
  • You put pre_get_posts code in functions.php to modify archive main queries before they are run. The results of the main query determine what template WP loads. If the main query has no page 2, your archive template will never be loaded in that case to run your custom query. – Milo Apr 25 '17 at 20:49
  • @Milo thank you, that helps, greatly appreciate the insight. – Ameer Bacchus Apr 27 '17 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.