0

This question already has an answer here:

I'm using a custom query to get all CPT posts for the author in author.php. I have a lot of authors and a lot of content so I'm using the wp pagenavi plugin to paginate between these posts.

For some reason the pagination is giving me 404 errors on a lot of these pages. The pages that throw 404s is always the last or the two last pages of the pagination.

So for example /author/steve/page/8/ will work, while /author/steve/page/9/ and /author/steve/page/10/ will not.

I believe the pagenavi plugin handles 'paged' => $paged, but i've also tried setting $paged in the file: $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;, but with no difference.

My global post_per_page (in the dashboard setting) is 6 posts per page. Changing 'posts_per_page' => 4 in the query to 'posts_per_page' => 6, fixes the 404 issue, but is more of a dirty fix as I need to only display 4 posts per page.

Below is my query:

<?php
$author_id = get_the_author_meta( 'ID' );

if ( have_posts() ) : 

the_post();
rewind_posts();

$author_details = $wp_query->get_queried_object();
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query(array(
  'post_type'=> 'publikasjoner',
  'paged' => $paged,
  'posts_per_page' => 4,
  'meta_key' => 'rapportnummer',
  'orderby' => 'meta_value_num',
  'order' => 'DSC',
  'author_name' => $curauth->user_nicename
));

while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

// Post content

<?php endwhile; endif;

if(function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $wp_query ) ); }

$wp_query = null; $wp_query = $temp; ?>

marked as duplicate by Pieter Goosen, mor7ifer, Mayeenul Islam, cybmeta, kaiser Feb 18 '15 at 0:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • This question relates to the use of the wp-pagenavi plugin and therefore is better asked on that plugins forums. wordpress.org/support/plugin/wp-pagenavi – Brad Dalton Feb 3 '15 at 15:03
  • 2
    404s on archive pages are determined by the main query, modify that and don't use a custom query. – Milo Feb 3 '15 at 15:11
  • @BradDalton This has nothing to do with the pagination plugin. Please see the post I marked that this one is a duplicate to. That is the cause of this issue :-) – Pieter Goosen Feb 3 '15 at 15:22
  • Depends which way you look at it. He wants to use wp-pagenavi as well. – Brad Dalton Feb 3 '15 at 16:25
  • 1
    @BradDalton it doesn't matter, even if you use core pagination functions, a plugin or your own custom function, it will never work. The main query will always 404 if you have more pages in a custom query than the main query and you go to a page that does not exists in the main query but does in the custom query – Pieter Goosen Feb 3 '15 at 16:54
0

Exactly what @Pieter Goosen said. Not only are you creating havoc for WordPress, you're adding unnecessary load on the server by introducing more queries on top of the main query:

function wpse_176933_custom_authors_archive( $wp_query ) {
    if ( $wp_query->is_main_query() && $wp_query->is_author() ) {
        $wp_query->set( 'posts_per_page', 4 );
        $wp_query->set( 'post_type', 'publikasjoner' );
        $wp_query->set( 'meta_key', 'rapportnummer' );
        $wp_query->set( 'orderby', 'meta_value_num' );
        $wp_query->set( 'order', 'DESC' );
    }
}

add_action( 'pre_get_posts', 'wpse_176933_custom_authors_archive' );

Add this hook to your functions.php, remove any custom queries from author.php, and replace your custom loop with the main loop functions (the_loop(), the_post() etc.).

Not the answer you're looking for? Browse other questions tagged or ask your own question.