Pagination not working on custom query

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

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

Leave a Comment