Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm currently displaying a list of all users using the following code. The only problem is that I need to paginate and only display 10 users at a time. I'm familiar with pagination using CPT's and Posts but not with users.

Is this possible? If so where would I start?

    <?php
/**
 * @package WordPress
 * @subpackage themename
 * Template Name: Vendors Page
 */

get_header(); ?>
<?php 
    $url = $_SERVER['REQUEST_URI'];
    $tokens = explode('/', $url);
    $usernameCheck = $tokens[sizeof($tokens)-2];
    if ($usernameCheck != 'vendors') {
        $username = $usernameCheck;
        $username = str_replace("-", " ", $username);
    }

    if(isset($_POST['gform_submit'])):
?>
    <script>
        jQuery(document).ready(function(){
            jQuery('div.profileEdit').show();
        });
    </script>
<? endif; ?>
<?php 
    $currentUserID = get_current_user_id(); 
    $page = (!empty($_GET['userp'])) ? $_GET['userp'] : 1;
    $per_page = 10;
    $offset = ( ($page -1) * $per_page);
?> 
<div id="main">
        <div id="primary">
            <div id="content">

                <?php the_post(); ?>
                <div class="contentLeft">

                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?> role="article">

                    <h1 class="entry-title"><?php if (!empty($username)) {echo $username; ?> <a class="profileEdit">Click Here To Edit Your Profile</a><?php }  else {the_title();} ?></h1>

                    <?php if (!empty($username)) { ?> 

                    <?php
                            // WP_User_Query arguments
                            $args = array (
                                'exclude'        => array( 1 ),
                                'count_total'    => true,
                                'fields'         => 'all_with_meta',
                                'meta_query'     => array(
                                    array(
                                        'key'       => 'user_business',
                                        'value'     => $username,
                                        'compare'   => 'LIKE',
                                    ),
                                ),

                            );

                            // The User Query
                            $user_query = new WP_User_Query( $args );

                            // The User Loop
                            if ( ! empty( $user_query->results ) ) {
                                foreach ( $user_query->results as $user ) { ?>
                            <?php if ( $currentUserID == $user->ID ) { ?><div class="profileEdit"><?php echo do_shortcode('[gravityform id="4" title="false" description="false"]'); ?></div><?php } ?> 
                            <div class="profileLeft">
                                <?php if (isset($user->user_pic)) { ?>
                                                <img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic; ?>&w=235&h=147" title="<?php echo $user->user_business; ?>" alt="<?php echo $user->user_business; ?>" class="profilePic" />
                                <?php } else { $imgURL = get_template_directory_uri() . "/images/vendor_placeholder.jpg"; ?>
                                                <img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $imgURL; ?>&w=235&h=147&zc=2" title="<?php echo $user->user_business; ?>" alt="<?php echo $user->user_business; ?>" class="profilePic" />
                                <?php } ?>
                                <h6>Owner Name:</h6>
                                <?php echo $user->first_name; ?>&nbsp;<?php echo $user->last_name; ?><br /><br />

                                <?php if ($user->user_address_show == 'Yes') { ?>
                                    <h6>Address:</h6>
                                    <?php echo $user->user_address_street; ?><br /><?php echo $user->user_address_city; ?>,&nbsp;KS&nbsp;<?php echo $user->user_address_zip; ?><br /><br />
                                <?php } ?>

                                <h6>Phone:</h6>
                                <?php echo $user->user_phone; ?>

                                <a href="mailto:<?php echo $user->user_email; ?>" class="contactVendor">Contact Us Via Email</a>

                                <h6>Markets We Attend:</h6>
                                <?php if (isset($user->we_attend_east)) { ?><div class="location east"><strong>East Wichita</strong><br />Green Acres Market</div><?php } ?>
                                <?php if (isset($user->we_attend_west)) { ?><div class="location west"><strong>West Wichita</strong><br />Sedgwick County Ext. Office</div><?php } ?>
                                <?php if (isset($user->we_attend_delano)) { ?><div class="location delano"><strong>Delano in Wichita</strong><br />Lawrence Dumont Parking lot</div><?php } ?>
                                <?php if (isset($user->we_attend_derby)) { ?><div class="location derby"><strong>Derby, Kansas</strong></div><?php } ?>

                                <?php if (isset($user->attend_this_week_east) || isset($user->attend_this_week_wast) || isset($user->attend_this_week_delano) || isset($user->attend_this_week_derby)) { ?>
                                <div class="attend">We will be at the 
                                <?php if (isset($user->attend_this_week_east)): ?>East Wichita Market<?php if (isset($user->attend_this_week_west) || isset($user->attend_this_week_delano) || isset($user->attend_this_week_derby)): ?>,<?php endif; ?><?php endif; ?>
                                <?php if (isset($user->attend_this_week_west)): ?>West Wichita Market<?php if (isset($user->attend_this_week_delano) || isset($user->attend_this_week_derby)): ?>,<?php endif; ?><?php endif; ?>
                                <?php if (isset($user->attend_this_week_delano)): ?>Delano in Wichita Market<?php if (isset($user->attend_this_week_derby)): ?><?php endif; ?><?php if (isset($user->attend_this_week_derby)): ?>&nbsp;and<?php endif; ?><?php endif; ?>
                                <?php if (isset($user->attend_this_week_derby)): ?>Derby, Kansas Market<?php endif; ?>
                                this week!
                                </div>
                                <?php } ?>

                                <h6>Items sold at market:</h6>
                                <ul class="profileItemsSold">
                                    <?php if (isset($user->product_1)){ ?><li><?php echo $user->product_1; ?></li><?php } ?>
                                    <?php if (isset($user->product_2)){ ?><li><?php echo $user->product_2; ?></li><?php } ?>
                                    <?php if (isset($user->product_3)){ ?><li><?php echo $user->product_3; ?></li><?php } ?>
                                    <?php if (isset($user->product_4)){ ?><li><?php echo $user->product_4; ?></li><?php } ?>
                                    <?php if (isset($user->product_5)){ ?><li><?php echo $user->product_5; ?></li><?php } ?>
                                </ul>

                                <?php if (isset($user->user_organic)){ ?>
                                    <div class="organic">Organic | Chemical Free</div>
                                <?php } ?>

                                <?php if (isset($user->user_producer)){ ?>
                                    <div class="producer">100% Producer</div>
                                <?php } ?>



                            </div>

                            <div class="profileRight">
                                <h6>About Us:</h6>
                                <p><?php echo $user->user_about; ?></p>
                            </div>

                            <div class="profileBottom">
                                <?php if (isset($user->user_profile_pic)){ ?><h1 class="entry-title">View Our Photos</h1><?php } ?>

                                <?php if (isset($user->user_profile_pic)){ ?>
                                    <div id="userPicsContainer">
                                        <img src="<?php bloginfo('template_url'); ?>/images/user_pics_prev.png" class="prev"/>
                                        <img src="<?php bloginfo('template_url'); ?>/images/user_pics_next.png" class="next"/>
                                        <div class="targetDiv">
                                            <ul>
                                                <li><a href="<?php echo $user->user_profile_pic; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_profile_pic_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_profile_pic; ?>&h=134&w=119" alt="<?php echo $user->user_profile_pic_c; ?>" title="<?php echo $user->user_profile_pic_c; ?>" /></a></li>

                                                <?php if (isset($user->user_pic_1)){ ?><li><a href="<?php echo $user->user_pic_1; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_1_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_1; ?>&h=134&w=119" alt="<?php echo $user->user_pic_1_c; ?>" title="<?php echo $user->user_pic_1_c; ?>" /></a></li><?php } ?>

                                                <?php if (isset($user->user_pic_2)){ ?><li><a href="<?php echo $user->user_pic_2; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_2_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_2; ?>&h=134&w=119" alt="<?php echo $user->user_pic_2_c; ?>" title="<?php echo $user->user_pic_2_c; ?>" /></a></li><?php } ?>

                                                <?php if (isset($user->user_pic_3)){ ?><li><a href="<?php echo $user->user_pic_3; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_3_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_3; ?>&h=134&w=119" alt="<?php echo $user->user_pic_3_c; ?>" title="<?php echo $user->user_pic_3_c; ?>" /></a></li><?php } ?>

                                                <?php if (isset($user->user_pic_4)){ ?><li><a href="<?php echo $user->user_pic_4; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_4_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_4; ?>&h=134&w=119" alt="<?php echo $user->user_pic_4_c; ?>" title="<?php echo $user->user_pic_4_c; ?>" /></a></li><?php } ?>

                                                <?php if (isset($user->user_pic_5)){ ?><li><a href="<?php echo $user->user_pic_5; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_5_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_5; ?>&h=134&w=119" alt="<?php echo $user->user_pic_5_c; ?>" title="<?php echo $user->user_pic_5_c; ?>" /></a></li><?php } ?>

                                                <?php if (isset($user->user_pic_6)){ ?><li><a href="<?php echo $user->user_pic_6; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_6_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_6; ?>&h=134&w=119" alt="<?php echo $user->user_pic_6_c; ?>" title="<?php echo $user->user_pic_6_c; ?>" /></a></li><?php } ?>

                                                <?php if (isset($user->user_pic_7)){ ?><li><a href="<?php echo $user->user_pic_7; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_7_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_7; ?>&h=134&w=119" alt="<?php echo $user->user_pic_7_c; ?>" title="<?php echo $user->user_pic_7_c; ?>" /></a></li><?php } ?>

                                                <?php if (isset($user->user_pic_8)){ ?><li><a href="<?php echo $user->user_pic_8; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_8_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_8; ?>&h=134&w=119" alt="<?php echo $user->user_pic_8_c; ?>" title="<?php echo $user->user_pic_8_c; ?>" /></a></li><?php } ?>

                                                <?php if (isset($user->user_pic_9)){ ?><li><a href="<?php echo $user->user_pic_9; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_9_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_9; ?>&h=134&w=119" alt="<?php echo $user->user_pic_9_c; ?>" title="<?php echo $user->user_pic_9_c; ?>" /></a></li><?php } ?>
                                            </ul>
                                        </div>
                                    </div>
                                <?php } ?>

                                <?php if (isset($user->user_twitter) || isset($user->user_facebook)){ ?><h1 class="entry-title">Socialize With Us</h1><?php } ?>

                                <?php if (isset($user->user_twitter)){ ?>
                                    <div id="twitterUserContainer">
                                        <img src="<?php bloginfo('template_url'); ?>/images/twitter_prev.png" class="prev"/>
                                        <img src="<?php bloginfo('template_url'); ?>/images/twitter_next.png" class="next"/>
                                        <div class="targetDiv">
                                            <?php echo do_shortcode('[twitter-user]'); ?>
                                        </div>
                                    </div>
                                    <br style="clear:both;" />
                                <?php } ?>

                                <?php if (isset($user->user_facebook)){ ?>
                                    <iframe src="//www.facebook.com/plugins/likebox.php?href=<?php echo urlencode($user->user_facebook); ?>&amp;width=588&amp;height=590&amp;show_faces=true&amp;colorscheme=light&amp;stream=true&amp;show_border=true&amp;header=true&amp;appId=172839226207438" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:588px; height:590px;" allowTransparency="true"></iframe>
                                <?php } ?>
                            </div>

                            <?php } } ?>

















                    <?php } else { ?>






                        <?php
                            // WP_User_Query arguments
                            $args = array (
                                'exclude'        => array( 1 ),
                                'count_total'    => true,
                                'fields'         => 'all_with_meta',
                                'number' => $per_page,
                                'offset' => $offset,
                            );


                            // The User Query
                            $user_query = new WP_User_Query( $args );

                            // The User Loop
                            if ( ! empty( $user_query->results ) ) {
                                foreach ( $user_query->results as $user ) { ?>

                                    <div class="vendorContainer">
                                        <a href="<?php bloginfo('url'); ?>/vendors/<?php echo str_replace(" ", "-", strtolower($user->user_business)); ?>">
                                            <?php if (isset($user->user_pic)) { ?>
                                                <img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic; ?>&w=235&h=147" title="<?php echo $user->user_business; ?>" alt="<?php echo $user->user_business; ?>" />
                                            <?php } else { $imgURL = get_template_directory_uri() . "/images/vendor_placeholder.jpg"; ?>

                                                <img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $imgURL; ?>&w=235&h=147&zc=2" title="<?php echo $user->user_business; ?>" alt="<?php echo $user->user_business; ?>" />
                                            <?php } ?></a>
                                            <div class="vendorContent">
                                                <h5><?php echo $user->user_business; ?><a href="<?php bloginfo('url'); ?>/vendors/<?php echo str_replace(" ", "-", strtolower($user->user_business)); ?>" class="viewProfile">View Profile</a></h5>
                                                <strong>Item(s) sold at market:</strong>
                                                <ul><?php $productCounter = 0; ?>
                                                    <?php if (isset($user->product_1) && $productCounter == 0) { $productCounter = 1; ?><li><?php echo $user->product_1; ?></li><?php } ?>
                                                    <?php if (isset($user->product_2) && $productCounter == 0) { $productCounter = 1; ?><li><?php echo $user->product_2; ?></li><?php } ?>
                                                    <?php if (isset($user->product_3) && $productCounter == 0) { $productCounter = 1; ?><li><?php echo $user->product_3; ?></li><?php } ?>
                                                    <?php if (isset($user->product_4) && $productCounter == 0) { $productCounter = 1; ?><li><?php echo $user->product_4; ?></li><?php } ?>
                                                    <?php if (isset($user->product_5) && $productCounter == 0) { $productCounter = 1; ?><li><?php echo $user->product_5; ?></li><?php } ?>
                                                </ul>
                                                <strong>Market(s) we attend:</strong><?php $marketCounter = 0; ?>
                                                <?php if (isset($user->we_attend_east) && $marketCounter != 2) { $marketCounter++ ?><div class="location east"><strong>East Wichita</strong><br />Green Acres Market</div><?php } ?>
                                                <?php if (isset($user->we_attend_west) && $marketCounter != 2) { $marketCounter++ ?><div class="location west"><strong>West Wichita</strong><br />Sedgwick County Ext. Office</div><?php } ?>
                                                <?php if (isset($user->we_attend_delano) && $marketCounter != 2) { $marketCounter++ ?><div class="location delano"><strong>Delano in Wichita</strong><br />Lawrence Dumont Parking lot</div><?php } ?>
                                                <?php if (isset($user->we_attend_derby) && $marketCounter != 2) { $marketCounter++ ?><div class="location derby"><strong>Derby, Kansas</strong></div><?php } ?>
                                            </div>
                                    </div>

                            <?php } } ?>







                    <?php } ?>

                <?php
                $page_args = array(
    'base'         => get_permalink( get_the_ID() ). '%_%',
    'format'       => add_query_arg(array('userp' => '%#%')),
    'total'        => ceil($users->total_users / $per_page),
    'current'      => $page,
    'show_all'     => True,
    'end_size'     => 2,
    'mid_size'     => 2,
    'prev_next'    => True,
    'prev_text'    => __('« Previous'),
    'next_text'    => __('Next »'),
    'type'         => 'plain',
);

echo paginate_links($page_args);
?>

                </article><!-- #post-<?php the_ID(); ?> -->
                </div>
                <?php if (!empty($username)) { ?><div class="contentRight"><?php if ( dynamic_sidebar('profile-sidebar') ) : else : ?><?php endif; ?></div><?php } else { ?><?php get_sidebar(); ?><?php } ?>
                </div>
            </div><!-- #content -->
        </div><!-- #primary -->
<?php get_footer(); ?>
share|improve this question
    
You should be able to use the number an offset args to limit the results to 10 then offset the pages based on page number. Not sure if this is the best way but should work. Alternatively, you could also do paging in your foreach and only get first 10 then do paging there. –  BandonRandon Jun 27 '13 at 22:19
add comment

2 Answers

As BandonRandon mentioned in the comments you can use the offset and number to query 10 users at a time.

Here's a simplified version of what I do in my Simple User Listing plugin which lists users with built-in pagination and the ability for customizing the list output in your theme, so it might save you some trouble.

// Get Query Var for pagination. This already exists in WordPress
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;

// Calculate the offset (i.e. how many users we should skip)
$offset = ($page - 1) * $number;

// users per page
$number = 10;

$args2 =
    array(
    'offset' => $offset,
    'number' => $number,
);

$sul_users = new WP_User_Query( $args );
share|improve this answer
    
I've updated the code in the original post to show what I'm working with now. The pagination works by using the userp variable in the url but Paginate Links is not working. –  user741166 Jul 10 '13 at 14:33
add comment

Here is a solution with working pagination:

$page = (!empty($_GET['userp'])) ? $_GET['userp'] : 1;
$per_page = 10;
$offset = ( ($page -1) * $per_page);

$args = array(
    'fields' => 'all_with_meta',
    'number' => $per_page,
    'offset' => $offset,
);

$users = new WP_User_Query( $args );

foreach( $users->results as $user ) { 
  // this is your foreach loop
  // simplified for my convenience
  echo $user->user_login;
  echo '<br>';
}

$page_args = array(
    'base'         => get_permalink( get_the_ID() ). '%_%',
    'format'       => add_query_arg(array('userp' => '%#%')),
    'total'        => ceil($users->total_users / $per_page),
    'current'      => $page,
    'show_all'     => True,
    'end_size'     => 2,
    'mid_size'     => 2,
    'prev_next'    => True,
    'prev_text'    => __('« Previous'),
    'next_text'    => __('Next »'),
    'type'         => 'plain',
);

echo paginate_links($page_args);

The key elements, as already stated, are the offset and the number.

Look at paginate_links for an explanation of the parameters if you need to adjust the pagination.

See WP_User_Query if you need to adjust the query itself.

share|improve this answer
    
I was unable to get this to work, the links never appeared –  user741166 Jul 3 '13 at 14:38
    
The links will not appear if $per_page is >= the number of users. Otherwise it works fine when I test it. It does depend somewhat on context. get_the_ID() won't work on archive pages, for example. –  s_ha_dum Jul 3 '13 at 14:56
    
Neat. I guess I will have to try to work paginate_links() into my user listing plugin. –  helgatheviking Jul 11 '13 at 4:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.