Paginate get related post by author function

As I already stated in a comment to your answer, you should never make use of query_posts

Note: This function isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

When you need paginated queries, WP_Query is the way to go as it returns all the necessary info for the correct calculations for pagination to work correctly. The other advantage here with using WP_Query is that results are cached making them a bit faster as well

You have to do two things here:

  • Set the paged parameter in your query arguments

  • Set the $max_pages parameter in next_posts_link()

You can try something like this: (CAVEAT: This is untested)

function get_related_author_posts() {

    global $authordata, $post;

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $args = array( 
        'author'            => $authordata->ID, 
        'post_type'         => 'kenniscentrum', 
        'post__not_in'      => array( $post->ID ), 
        'posts_per_page'    => 5,
        'paged'             => $paged
    );
    $authors_posts = new WP_Query( $args );

    $output="";

    if( $authors_posts->have_posts() ) {

        $output="<ul>";

        while( $authors_posts->have_posts() ) {
            $authors_posts->the_post();

            $output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a>' . get_the_excerpt() . '</li>';

        }

        $output .= '</ul>';

        $output .= '<div class="nav-previous"> '. get_next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts' ), $authors_posts->max_num_pages) .'</div>';
        $output .= '<div class="nav-next"> '. get_previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>' ) ) .'</div>';


        wp_reset_postdata();
    }

    return $output;

}

Leave a Comment