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/?searchcategory=Government#038;category=Governmentpaged=2

Topic wp-user-query paged posts Wordpress search

Category Web


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
) );
?>

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


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.