0

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; ?>
5
  • 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 Feb 3, 2015 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, 2015 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 :-) Feb 3, 2015 at 15:22
  • Depends which way you look at it. He wants to use wp-pagenavi as well. Feb 3, 2015 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 Feb 3, 2015 at 16:54

1 Answer 1

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.