How to display next and prev pagination links with WP_User_Query?

Looking through the pagination functions available in WordPress, most seem to be associated with posts. paginate_links() seems to be the only function which will work with WP_User_Query. However, when I use that I get numbered pagination:

echo paginate_links( array(
    'base'      => get_pagenum_link( 1 ) . '%_%',
    'current'   => max( 1, get_query_var( 'paged' ) ),
    'format'    => 'page/%#%/',
    'prev_next' => true,
    'total'     => intval( $wp_user_query->total_users / $number ) + 1
) );

How can I output “Prev” and “Next” pagination links that work with WP_User_Query? Note, I don’t want to output numbered links such as 1, 2, 3 and so on.

1 Answer
1

I’m not aware of any generic helper – all the post-related navigation functions seem to be tied into the global WP_Query instance. The only real useful function at your disposal is get_pagenum_link:

$paged = max( 1, get_query_var( 'paged' ) );

if ( $number * $paged < $wp_user_query->total_users ) {
    printf( '<a href="https://wordpress.stackexchange.com/questions/267947/%s">Next</a>', get_pagenum_link( $paged + 1 ) ); 
}

if ( $paged > 1 ) {
    printf( '<a href="https://wordpress.stackexchange.com/questions/267947/%s">Back</a>', get_pagenum_link( $paged - 1 ) ); 
}

Note the function returns escaped strings by default, so no need for esc_url.

Leave a Comment