Related posts by author pagination not working on the production site

As I said, this whole setup you are after is not possible natively with pretty permalinks. Your setup probably works with default permalink structure as both queries (the main query and your custom query) read these permalinks in the same way. When you switch to pretty permalinks, the two queries on the single page interpret the URL differently causing one or the other to fail when you try to paginate your custom query

Single pages was never meant to be paginated in this manner, specially using pretty permalinks. I have gone and played around with a couple of ideas, and the best way to accomplish this is

  • To write your own pagination functions that can read the page number from the URL

  • Write your own function that can append the page number to the URL, something like adding /2/ to the URL of single pages.

I must stress, if you are paginating single post with <!--nextpage-->, your post will also paginate together with your custom query. Also, if your permalink structure is not set to /%postname%/, the code will fail and display an error message through wp_die()

THE CODE

Here is the code that will get the next/previous page and also add the pagenumber to the URL.

function get_single_pagination_link( $pagenum = 1 ) {
    global $wp_rewrite;

    if( is_singular() && $wp_rewrite->permalink_structure == '/%postname%/') {

        $pagenum = (int) $pagenum;

        $post_id = get_queried_object_id();
        $request = get_permalink( $post_id );

        if ( $pagenum > 1 ) {
            $request = trailingslashit( $request ) . user_trailingslashit( $pagenum );
        }

        return esc_url( $request );

    }else{

        wp_die( '<strong>The function get_single_pagination_link() requires that your permalinks are set to /%postname%/</strong>' );

    }
}

You can use this function as follow to get the link for any page in the single page when paginating your custom query

get_single_pagination_link( 'pagenumber_of_previous_or_next_page' );

Again, as I said, there is no pagination function that will be able to paginate your custom query or read pagenumbers from the URL, so you have to write your own.

Here is my idea of creating links to the next and previous pages in your custom query. If you look closely, you will see how I have used the previous declared function get_single_pagination_link() to get the links to the next and previous pages

function get_next_single_page_link ( $label = null, $max_page = 0 ) {
    global $wp_query;

    if ( !$max_page ) {
        $max_page = $wp_query->max_num_pages;
    }

    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

    if( is_singular() ) {

        $next_page = intval($paged) + 1;

        if ( null === $label ) {
            $label = __( 'Next Page &raquo;' );
        }

        if ( ( $next_page <= $max_page ) ) {
            return '<a href="' . get_single_pagination_link( $next_page ) . '">' . $label . '</a>';
        }

    }
}

function get_previous_single_page_link( $label = null ) {

    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

    if( is_singular() ) {

        $prev_page = intval($paged) - 1;

        if ( null === $label ) {
            $label = __( '&laquo; Previous Page' );
        }

        if ( ( $prev_page > 0 ) ) {
            return '<a href="' . get_single_pagination_link( $prev_page ) . '">' . $label . '</a>';
        }

    }

}

You can now use this two functions in your code to paginate your custom query. Both functions work exactly the same as get_next_posts_link() and get_previous_posts_link() and uses the same exact parameters, so you’ll need to keep in mind that you need to pass the $max_page parameter for your custom query

Here is your custom query with these functions.

function get_related_author_posts() {

    global $authordata, $post;

    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

    $args = array( 
        'posts_per_page'    => 2,
        '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 .= get_previous_single_page_link();
        $output .= get_next_single_page_link( null , $authors_posts->max_num_pages );

        wp_reset_postdata();
    }

    return $output;

}

Leave a Comment