I have used a custom paging navigation using the below mentioned code:

if ( ! function_exists( 'thirdrock_paging_nav' ) ) :
/**
 * Display navigation to next/previous set of posts when applicable.
 *
 * @return void
 */
function thirdrock_paging_nav() {
    // Don't print empty markup if there's only one page.
    if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
        return;
    }

    $paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
    $pagenum_link = html_entity_decode( get_pagenum_link() );
    $query_args   = array();
    $url_parts    = explode( '?', $pagenum_link );
    if ( isset( $url_parts[1] ) ) {
        wp_parse_str( $url_parts[1], $query_args );
    }
    $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
    $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
    $format  = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
    $format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
    // Set up paginated links.
    $links = paginate_links( array(
        'base'     => $pagenum_link,
        'format'   => $format,
        'total'    => $GLOBALS['wp_query']->max_num_pages,
        'current'  => $paged,
        'mid_size' => 2,
        'add_args' => array_map( 'urlencode', $query_args ),
        'prev_text' => '<i class="fa fa-backward"></i>',
        'next_text' => '<i class="fa fa-forward"></i>',
        'type'      => 'list',
    ) );
    if ( $links ) :
    ?>
    <nav class="navigation paging-navigation" role="navigation">
        <h1 class="screen-reader-text"><?php esc_html_e( 'Posts navigation', 'thirdrock' ); ?></h1>
            <?php echo $links; ?>
    </nav><!-- .navigation -->
    <?php
    endif;
}
endif;

It works fine with the WordPress loop:

<?php
if ( have_posts() ) :

    while ( have_posts() ) : the_post();

        get_template_part( 'template-parts/content', get_post_format() );

    endwhile;

    thirdrock_paging_nav();

else :

    get_template_part( 'template-parts/content', 'none' );

endif;

?>

But it does not yield anything if I use it with WP_Query. I have added the $paged variable too. Note that this pagination is going to be used on single posts page.

<?php 
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$relatedArgs = array(
    'post_type' => 'post',
    'posts_per_page' => 3,
    'orderby'   => 'rand',
    'paged' => $paged       
);
$relatedQuery = new WP_Query($relatedArgs);
if ($relatedQuery->have_posts()):?>
    <?php 
    while ($relatedQuery->have_posts()) : $relatedQuery->the_post();        
    ?>
    some html here
    <?php endwhile;
    thirdrock_paging_nav();
    wp_reset_query(); ?>
</div><!-- .row -->

<?php else: ?>
<p>No related posts.</p>
<?php endif; ?>  

So, may I know why the pagination does not work with WP_Query here.

Read more here: Custom pagination not working with WP_Query


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: