Display users by role

What I understand from your code: You want to list users with author role.

You’re in the right direction, however, following are some tips to make your code better.

  • Use role id in lowercase syntax. Change Author to author for role property.
  • Get the results for once and avoid repeated calls. Save the results like $authors = $user_query->get_the_results(); then, use if (!empty($authors)) { ... } to check and foreach($authors as $author) {...} to loop through.
  • Wrap the actual results in <ol> or <ul> or other needed element, so, start the <ol> tag only if results are found and close the </ol> tag right after foreach loop. For the else content, use other HTML element such as <p> with a error code style class etc.
  • Consider pagination for high number of users. Consider the case when the total matched users go up to 100+ to 1000s over the time.

After applying the given tips, the code is as follows: –

Without pagination

<?php
$args = array(
    'role'    => 'author', // changed Author to author
    'orderby' => 'user_nicename',
    'order'   => 'ASC'
);
$authors_query = new WP_User_Query($args); // enhanced the variable name from $user_query to $authors_query 
$authors = $authors_query->get_results(); // saved the results for once to avoid repeated calls later on
?>

<?php if (!empty($authors)) { ?>
    <table> <!-- Replaced ol to table element -->
        <thead>
            <th>Name</th>
            <th>Details</th>
            <th>URL</th>
        </thead>
        <?php foreach ($authors as $user ) { ?>
            <tr>
                <td><?php echo $user->display_name ?></td>
                <td>
                    <?php echo $user->ID ?> | <?php echo $user->user_login ?> | <?php echo $user->user_email ?> 
                </td>
                <td><?php echo $user->user_url ?></td>
            </tr>
        <?php } //endforeach ?>
        </table>
<?php } else { ?>
    <p class="no-results-notice">No authors found.</p>
<?php } ?>

With pagination

<?php

//Pagination
$page = isset($_GET['show_authors']) ? $_GET['show_authors'] : 1;
$authors_per_page = 20;  // you can change the number as needed
$total_pages = 1; // initial value; keep it one; we will update this after having actual number of users from query
$offset = $authors_per_page * ($page - 1);


$args = array(
    'role'    => 'author', // changed Author to author
    'orderby' => 'user_nicename',
    'order'   => 'ASC'
);
$authors_query = new WP_User_Query($args); // enhanced the variable name from $user_query to $authors_query 
$authors = $authors_query->get_results(); // saved the results for once to avoid repeated calls later on

// Updating variables for pagination.
$total_authors = $authors_query->get_total(); 
$total_pages = ceil($total_authors / $authors_per_page); 

?>

<?php if (!empty($authors)) { ?>
    <h2>We have <?php echo $total_authors; ?> authors in our website. </h2>
    <table> <!-- Replaced ol to table element -->
        <thead>
            <th>Name</th>
            <th>Details</th>
            <th>URL</th>
        </thead>
        <?php foreach ($authors as $user ) { ?>
            <tr>
                <td><?php echo $user->display_name ?></td>
                <td>
                    <?php echo $user->ID ?> | <?php echo $user->user_login ?> | <?php echo $user->user_email ?> 
                </td>
                <td><?php echo $user->user_url ?></td>
            </tr>
        <?php } //endforeach ?>
        </table>

        <!-- Display Pagination Links -->
        <div class="authors-pagination">
            <?php                 
                echo paginate_links( array(
                    'base'         => add_query_arg( 'show_authors', '%#%' ) ,
                    'total'        => $total_pages,
                    'current'      => $page,
                    'format'       => '',
                    'type'         => 'plain',
                    'end_size'     => 1,
                    'mid_size'     => 1,
                    'prev_next'    => true,
                    'prev_text'    => sprintf( '<i></i> %1$s', __( 'Previous', 'text-domain' ) ),
                    'next_text'    => sprintf( '%1$s <i></i>', __( 'Next', 'text-domain' ) )
                ) );
            ?>
        </div>

<?php } else { ?>
    <p class="no-results-notice">No authors found.</p>
<?php } ?>