get_query_var(‘paged’) always returns empty

Putting this up after having hit a brick wall for possible solutions.

Have a site with quite a few custom post types and associated custom taxonomies. To help make things easy, I’m using a unified taxonomy.php template.

Read More

Within that template there are three loops. I would like all of them to run on the first page, after that, just the third loop. I understand how to make that work with is_paged() but there’s a bigger problem that I just cannot resolve.

In my $args I included $paged from

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;     

But $paged always returns as 1, now matter what page I am on. If I do a straight

    get_query_var('paged')

Then nothing is returned, no matter if I am on the first, second, third or etc page.

I’ve read that you need to reset each loops for this to work, and that’s a practice I’ve always followed.

I won’t include the whole page here, as this is a rather long and complex taxonomy. Instead I’ll show the base set up of the two loops.

NOTE: I can’t use pre_get_posts for this page as it affects all loops. Each of these loops have separate posts_per_page requirements and other aspects that need to be kept separate.

At the top of the template I have

    //this present duplication
        $do_not_duplicate = array();

        global $wp_query;
        $term = $wp_query->queried_object;

And I set up my loop after my args with

while ($new_query->have_posts()) : $new_query->the_post();

The third loop that I want paged has this code nearby

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;                                 
                $args=array(
                    'posts_per_page' => $total_posts,
                    'post__not_in' => array_merge($do_not_duplicate, $unwanted_post_ids ),
                    'paged' => $paged,
                    'tax_query' => array(
                            array(
                                'taxonomy'  => $term->taxonomy,
                                'field'     => 'slug',
                                'terms'     => $term->slug,
                                )
                            )
                    );

                // Re-run the query with the new arguments
                query_posts( $args );

I have been using wp_query rather than query_posts, but for my latest attempts that’s what I’ve had there. Would prefer to change it back.

Any ideas?

Related posts

Leave a Reply

5 comments

  1. Try to use the following:

    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    

    Instead of using:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    

    I had the same issue and it works for me.

  2. Do you have the “paged” parameter in the URL of the page(s) that you are trying to use get_query_var with? As far as I know get_query_var(“paged”) is explicitly returning the value of the URL param so you need to have a URL like this for it to work:

    /?paged=7
    

    Alternatively you could read the URL param using straight-up PHP:

    $_GET['paged']
    
  3. You should be able to take this straight from the main $wp_query variable, you could do a print_r()and check for the various arguments you’d need, like paged, posts_per_page etc etc. What information can you see on your template when doing this?

  4. I had the same problem, although specifically paginating using AJAX. get_query_var(‘paged’) returned an empty string, so $paged was always setting to “1”, and – although the pages were changing – the pagination links always showed “1” as the current page.

    To solve my issue, I just grabbed the page value passed through to my functions file ($_POST[‘page’]) by AJAX, called it $pagination_no, then changed the $paged formula to:

    $paged = ($pagination_no) ? $pagnination_no : 1;
    

    That did the trick.

  5. Use page, not paged, on 👉🏻static front page👈🏻

    ✅ The solution:

    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    

    ⭐️ A better syntax (see source):

    $paged = get_query_var('page', 1);
    

    🙋🏻 Inside the loop you can just use directly the global $page value (see docs):

    global $page;
    

    If you are on a static front page you have to use the page query variable instead of the paged one (see documentation).

    The global $page always returns an integer.

    🔍 A better approach:

    $paged = 1;
    if ( get_query_var('paged') ) $paged = get_query_var('paged');
    if ( get_query_var('page') ) $paged = get_query_var('page');
    

    This will gracefully fallback no matter where you use the code.

    Here an old reference that tries to solve the issue.