Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Using ajax pagination on WP_User_Query WordPress

  • REFUNDED

I have a custom query for users which works fine. But need to use ajax for the pagination.

Here is the code


$current_page = get_query_var('paged') ? (int) get_query_var('paged') : 1;
$users_per_page = 12;

$args = array (
'order' => 'ASC',
'orderby' => 'display_name',
'role' => 'client',
'paged' => $current_page,
'number' => $users_per_page
);

$wp_user_query = new WP_User_Query( $args );
$total_users = $wp_user_query->get_total();
$num_pages = ceil($total_users / $users_per_page);

$members = $wp_user_query->get_results();

if ( ! empty( $members ) ) {
echo '<div id="members">';

foreach ( $members as $member ) { ?>
<div class="member">/* Member info */</div>
<?php }

echo '</div>';
} else { ?>

<h3>No member found</h3>

<?php }

/* Pagination */
echo '<div class="pagination">';
echo paginate_links( array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%/',
'prev_text' => __('&laquo;'), // text for previous page
'next_text' => __('&raquo;'), // text for next page
'current' => max( 1, get_query_var('paged') ),
'total' => $num_pages,
) );
echo '</div>';

Answers (2)

2018-03-09

Arnav Joy answers:

Cab you please show me your site ?


User179805 comments:

Sorry. It is in local environment.


User179805 comments:

You can check here. It will be live for the next 4 hours

http://lucky-dove.w5.poopy.life/clients/

2018-03-09

Cesar Contreras answers:

you could use a plugin, I recommend "wp-pagenavi" is very good and easy to use

https://es-mx.wordpress.org/plugins/wp-pagenavi/


User179805 comments:

I don't think you got what I need. I need Ajax pagination. The pagination already works.


Cesar Contreras comments:

Try with this code, please


/* Pagination */
echo '<div class="pagination">';
echo paginate_links( array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged/%#%/',
'prev_text' => __('&laquo;'), // text for previous page
'next_text' => __('&raquo;'), // text for next page
'current' => max( 1, get_query_var('paged') ),
'total' => $num_pages,
) );
echo '</div>';


User179805 comments:

I have no problem with the pagination. it works fine. Please read the post.