3

I am trying to change pages when I search by category on my website. When I am not searching under a certain category I can switch pages fine but when I am searching for a category I am unable to switch pages.

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
        $orderby = ( get_query_var( 'orderby' ) ) ? absint( get_query_var( 'orderby' ) ) : 'display_name';
        if($_GET['search'] && !empty($_GET['search'])) {
            $search = $_GET['search'];
        }

        if($_GET['category'] && !empty($_GET['category'])) {
            $category = $_GET['category'];
        }

        $args = array(
            'orderby' => 'rand',
            'number' => 7,
            'paged' => $paged,
            'search' => '*'.esc_attr( $search ).'*',
            'meta_query' => array (
                array(
                    'key' => 'organization_category_2',
                    'value' => $category,
                    'compare' => 'like'
                )
            )
        );


        $user_query = new WP_User_Query($args);

And my pagination links:

<?php
              $total_user = $user_query->total_users;
              $total_pages=ceil($total_user/7);

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

  ?>

Whenever I try and do a search I get a url like this:

https://mywebsite.ca/directory/?search&category=Government#038;category=Government&paged=2

3 Answers 3

2

The pagination functions work off of the main query, so you'd need to use pre_get_posts instead of creating a new query for them to work.

But, you're using WP_User_Query, so the standard pagination system will never work for you. You're going to have to 'roll your own' pagination system here, and generate your own URLs manually, and set the page yourself based on the URL

2
  • If I remove the category=Government#038; and make the url look like this: https://mywebsite.ca/directory/?search&category=Government&paged=2 I am able to go on another page. How can I remove the duplicate that I am getting from it? Commented Mar 15, 2017 at 0:04
  • That would be because WordPress has more than 1 page of posts in its main query, but as we established, you aren't using the main post query, or even posts for that matter. I strongly suggest you read up on how the main query works, you appear to be missing required information
    – Tom J Nowell
    Commented Mar 15, 2017 at 14:36
1
if( $user_query->max_num_pages > 1 ) {

    $big = 999999;
    echo paginate_links( array(
        'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format'    => '?paged=%#%',
        'current'   => max( 1, $paged ),
        'total'     => $user_query->max_num_pages
    ) );
}

This might be useful for you

1

This is example from officialy WordPress Codex https://codex.wordpress.org/Function_Reference/paginate_links#Example_With_a_Custom_Query

Example of a custom query:

<?php
//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$args = array(
    'posts_per_page' => 5,
    'category_name' => 'gallery',
    'paged' => $paged,
);

$the_query = new WP_Query( $args );
?>
<!-- the loop etc.. -->

Example of paginate_links parameters adapted to the custom query above:

<?php
$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $the_query->max_num_pages
) );
?>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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