I using custom $wp_query for most of my archive pages like, taxonomy,index,category and author page. Everything working fine, except one weird thing - the author template page pagination failed to work. The pagination stop to work on page 2 it direct to the 404 page. Pagination on other archive page, works perfectly.

Here is my codes for author.php template :

<?php get_header(); ?>
<section id="primary">
<div id="content" role="main">

<?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

            <?php toolbox_content_nav( 'nav-above' ); ?>

            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
            'showposts'=> 2,
            'paged' => $paged,
            'post_type'=> array('cpt1','cpt2'));    

                    $query = new WP_Query( $args );
        <?php if ($query ->have_posts() ) : ?>
            <h1>Posts from <?php echo $curauth->first_name; ?> </h1>
              <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                                  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent    Link: <?php the_title(); ?>">
                                 <?php the_title(); ?></a>,
                                  <?php the_time('d M Y'); ?> in <?php the_category('&');?>

            <?php endwhile; ?>

            <?php wp_pagenavi( array( 'query' => $query) );  ?>

        <?php else : ?>

            <article id="post-0" class="post no-results not-found">
                <header class="entry-header">
                    <h1 class="entry-title"><?php _e( 'Nothing Found', 'toolbox' ); ?></h1>
                </header><!-- .entry-header -->

                <div class="entry-content">
                    <p><?php _e( 'It seems we can&rsquo;t find what you&rsquo;re looking for. Perhaps searching can help.', 'toolbox' ); ?></p>
                    <?php get_search_form(); ?>
                </div><!-- .entry-content -->
            </article><!-- #post-0 -->

        <?php endif; ?>

        </div><!-- #content -->
    </section><!-- #primary -->

     <?php get_sidebar(); ?>
     <?php get_footer(); ?>

I am using wp_pagenavi plugin for the pagination anyway. Apparently the wp_pagenavi knows there are pages (because it show more than 1 page at the pagination link), but when i goto page two, turns out no post was found (404 page).

I've googled this issue and applied all the suggestion that I searched, still no help. What is the problem?


You have a couple of issues here, the main one being that your query sets posts per page to two, but the number of pages available has no relationship to your custom query. If your "Blog pages show at most" under Reading settings is set to 10, and an author only has <= 10 posts, there is no second page.

The other issue you will discover is that you're not setting an author argument in your query, so you're not pulling posts from a specific author.

To solve both of these problems and also eliminate the waste of resources in creating secondary queries to replace default queries, you should remove all of this from your templates and instead use the pre_get_posts action in your functions.php. This way, pagination is handled automatically.

function wpa63043_author_pre_get( $query ) {
    if ( $query->is_author() && $query->is_main_query() ) :
        $query->set( 'posts_per_page', 2 );
        $query->set( 'post_type', array('cpt1','cpt2') );
add_action( 'pre_get_posts', 'wpa63043_author_pre_get' );
  • hi,just now I discovered that my menu from wp_nav_menu messed up with the code above. My custom menu was replaced by a new menu where display all of my created pages. why is this happened? and how to solve it? – dev-jim Aug 28 '12 at 16:39
  • using is_main_query() should only apply the action to the page's main query, it doesn't sound like this is responsible for that behavior. can you verify that the menu returns to normal if you comment out the add_action line? – Milo Aug 28 '12 at 16:43
  • yes...the menu become normal when I comment out the action. And I also discover that, this problem appeared to logged in user only. – dev-jim Aug 28 '12 at 16:46
  • Okay...figured it out. Turn out I need to add $query-> to is_author and is_main_query. and return to $query. Anyway, thanks again @Milo. – dev-jim Aug 28 '12 at 17:44

Try changing your page slug. If I remember correctly, it cannot be the same as custom post type.

@edit: I don't have time to check it right now, but here's what may cause this problem imho:

You have page with url yoursite.com/author. Your site can display single author, named "John" in example using this link: yoursite.com/author/john/. When you try to add navigation, this url is created: yoursite.com/author/page/2. WordPress assumes that word "page" in the example is actually the name of the author, which doesn't exist and thats why uyou get 404 error. If You change your url's to display single author John: yoursite.com/author/john and page with all authors listed to: yoursite.com/authors/ (added s), than you won't have url problems.

  • u mean change example.com/author/name to example/own-slug/name?I did tried this, but no luck as well. – dev-jim Aug 25 '12 at 17:36
  • Nope,the url that goes to page 2 is exactly yoursite.com/author/john/page/2, it append with the author name before the 'page' slug. Also I created a page to list all authors, but is still doesn't work. – dev-jim Aug 25 '12 at 18:07
  • @tc.k Have you tried using default permalinks? Is it working? You want to list all the authors from custom post type, with page using template author.php, is that right? Can You show us this page url before and after clicking on pagination links? – smogg Aug 25 '12 at 18:47
  • Yes, i tried using the default permalink,no luck. The author.php template is use to display the posts from particular author. eg. john, and all his posts (from custom post type). Before click on the link, it show mysite.com/author/john/ and when I clicked on page number 2, it goes to mysite.com/author/john/page/2. the site is under development now on my local machine. – dev-jim Aug 25 '12 at 18:58

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.