0

I have the following code which queries for all users (excluding admin) and sort them by the meta value user_business. When there are more than 10 users I have pagination which is currently working perfectly except no matter what 'userp' equals the pagination has page 1 always set as the current page. Can someone help me correct this issue?

    <?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);
    }
?>
<?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 str_replace("%27", "'", $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'     => str_replace("%27", "'", $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 ): ?><a href="<?php bloginfo('url'); ?>/update-your-profile/" class="profileEdit">Click Here To Edit Your Profile</a><?php endif; } } } ?></h1>

                    <?php if (!empty($username)) { ?> 
                     <?php wp_reset_query(); ?> 
                    <?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'     => str_replace("%27", "'", $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 ) { ?>
                            <div class="profileLeft">
                                <?php if (isset($user->user_profile_pic)) { ?>
                                                <img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_profile_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 } ?>

                                <?php if (isset($user->user_phone)){ ?>
                                    <h6>Phone:</h6>
                                    <?php echo $user->user_phone; ?>
                                <?php } ?>

                                <?php if (isset($user->user_booth)){ ?>
                                    <h6>Booth #:</h6>
                                    <?php echo $user->user_booth; ?>
                                <?php } ?>

                                <?php if (isset($user->website)){ ?>
                                    <a href="<?php echo $user->website; ?>" class="profileWebsite" target="_blank">View Our Website</a>
                                    <?php echo $user->website; ?>
                                <?php } ?>

                                <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 } ?>
                                </ul>

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

                                <?php if ($user->user_producer == 'yes'){ ?>
                                    <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,
                                'meta_key'       => 'user_business',
                                'query_id'       => 'wps_users',
                            );


                            // 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 } ?>
<div class="pagination">                
                <?php
                $page_args = array(
    'base'         => '%_%',
    'format'       => add_query_arg(array('userp' => '%#%')),
    'total'        => ceil($user_query->total_users / $per_page),
    'current'      => $page,
    'end_size'     => 2,
    'mid_size'     => 2,
    'prev_next'    => True,
    'prev_text'    => __('&#171; Previous'),
    'next_text'    => __('Next &#187;'),
    'type'         => 'plain',
);

echo paginate_links($page_args);
?>
</div>
                </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(); ?>
  • 1
    Could you var_dump $page variable just before <div class="pagination">? What will be the output? – Krzysiek Dróżdż Jul 12 '13 at 14:07
1

I ran your code, and the problem I see seems to be opposite what you describe-- Page 1 never sets as the current page. Assuming that is the issue, I understand the puzzlement. That one is tricky to work out. You need to set base to an absolute URL.

'base' => get_permalink( get_the_ID() ). '%_%',

Or something similar that works in the context your need. That is, get_the_ID() does not work in all contexts. The absolute URL is the key though.

  • It doesn't set 1 as current? But it's highlighted and has the correct class.. – Austin Biggs Jul 12 '13 at 15:36
0

After playing around with the $page variable I figured out that simply reassigning the $page variable right before paginate_links fixed everything.

    <?php
                $page = $_GET['userp'];
                $page_args = array(
    'base'         => '%_%',
    'format'       => add_query_arg(array('userp' => '%#%')),
    'total'        => ceil($user_query->total_users / $per_page),
    'current'      => $page,
    'end_size'     => 2,
    'mid_size'     => 2,
    'prev_next'    => True,
    'prev_text'    => __('&#171; Previous'),
    'next_text'    => __('Next &#187;'),
    'type'         => 'plain',
);

echo paginate_links($page_args);
?>
  • After doing so once you go to a page that isn't 1, the link for page 1 is set as the current page number. How can this be fixed? – Austin Biggs Jul 12 '13 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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