Maanstraat
Maanstraat

Reputation: 1251

Check if it is the starting variable in url or if it is the second one

I build a userlist with a pagination in WordPress but now i have build a filter with it and this is not totally working because of the ? or & in the url example:

Example: When a user clicks on a filter then the user will see the results. But when the user then use the pagination the user will get a 404 page because the pagination uses "?" instead of the "&".

url example: ?meta_value=keyvalue?paged=2

So how can i make it when you are on /users/ the pagination works with "?" and when you use the filter the pagination wil use "&" then instead of the "?".

My code:

<ul id="filter">
    <li>Filter op:</li>
    <li><a href="<?php echo '?meta_value=group01'; ?>">Group 01</a></li>
    <li><a href="<?php echo '?meta_value=group02'; ?>">Group 02</a></li>
    <li><a href="<?php echo '?meta_value=group03'; ?>">Group 03</a></li>
    <li><a href="<?php echo '?meta_value=group04'; ?>">Group 04</a></li>
</ul>

<?php
$no=10;// total no of author to display

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if($paged==1){
    $offset=0;  
}
else {
    $offset= ($paged-1)*$no;
}
$args = array(
    'meta_key' => 'usergroup',
    'meta_value' => $_GET['meta_value'],
    'number' => $no, 'offset' => $offset,
);

$user_query = new WP_User_Query( $args );
if ( !empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<div class="item">';
        echo '<a href="' . esc_html( $user->user_nicename ) .'">' . esc_html( $user->display_name ) . '</a>';
        echo '</div>';
    }
}
else {
    echo '<h4>No agents found.</h4>';
}

$total_user = $user_query->total_users;  
$total_pages=ceil($total_user/$no);

echo paginate_links(array(  
    'base' => get_pagenum_link(1) . '%_%',  
    'format' => '?paged=%#%',  
    'current' => $paged,  
    'total' => $total_pages,  
    'prev_text' => 'Previous',  
    'next_text' => 'Next',
    'type' => 'list'        
));  
?>

Upvotes: 0

Views: 33

Answers (1)

Sam Tolton
Sam Tolton

Reputation: 357

I would check to see if there is anything in $args['meta_value'] and if there is then change 'format' => '?paged=%#%', into 'format' => '&paged=%#%',.

You could do it like this:-

$symbol = empty($args['meta_value']) ? '?' : '&';

echo paginate_links(array(  
    'base' => get_pagenum_link(1) . '%_%',  
    'format' => $symbol.'paged=%#%',  
    'current' => $paged,  
    'total' => $total_pages,  
    'prev_text' => 'Previous',  
    'next_text' => 'Next',
    'type' => 'list'        
));  

Upvotes: 1

Related Questions