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.
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?
Try to use the following:
Instead of using:
I had the same issue and it works for me.
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:
Alternatively you could read the URL param using straight-up PHP:
You should be able to take this straight from the main
$wp_query
variable, you could do aprint_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?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:
That did the trick.
Use
page
, notpaged
, on ðð»static front pageðð»â The solution:
âï¸ A better syntax (see source):
ðð» Inside the loop you can just use directly the global
$page
value (see docs):If you are on a static front page you have to use the
page
query variable instead of thepaged
one (see documentation).The global
$page
always returns an integer.ð A better approach:
This will gracefully fallback no matter where you use the code.
Here an old reference that tries to solve the issue.