Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WP Query pagination outputs, but page links do not change the query. WordPress

  • SOLVED

I'm having issues with pagination. I'm using it on a wp_query.

The pagination outputs, and the links work, but the loop stays on the same page, posts never change.

Here is my code...

(i'm setting posts per page as 1 as I only have 3 posts)


<?php

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

$args = [
'paged' => $paged,
'post_type' => [
'purchase-order'
],
'posts_per_page' => 1
];

$query = new WP_Query($args);

if ( $query->have_posts() ) : ?>

<?php while ( $query->have_posts() ) : $query->the_post() ?>

<?=$query->post->post_title?>

<?php endwhile; ?>

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

<?php wp_reset_postdata(); ?>

<?php else : ?>

<p>Sorry, no results matched your criteria</p>

<?php endif; ?>


If I dump $paged, what ever page link I visit always returns 1.

If change $paged to 2 or 3 it the loop outputs the correct page.

I'm guessing

https://www.mysite.com/suppliers - $paged returns 1
https://www.mysite.com/suppliers/page/2 - $paged returns 1
https://www.mysite.com/suppliers/page/3 - $paged returns 1


If I dump `get_query_var( 'paged')` it just returns zero on any of those page links above.

What am I missing to get this working.

I do not want to use URL vars with `?page=3`, i want clean `/page/3` etc. I this possible?

Answers (4)

2021-03-25

Arnav Joy answers:

please read this

https://www.webdesignvista.com/how-to-make-pagination-work-with-wp_query-custom-loop-in-wordpress/

and this

https://njengah.com/custom-query-pagination-in-wordpress/


joshmoto comments:

None of these articles use paginate_links function

https://developer.wordpress.org/reference/functions/paginate_links/

I'm trying to get some thing like this to work...

« Prev 1 2 3 4 5 6 7 Next »

2021-03-25

Cesar Contreras answers:

Try removing the
],
'posts_per_page' => 1
];

or changing it to -1


joshmoto comments:

That displays all my posts and no pagination.

1 is just for testing, this will be 20 or something. I've only got 3 posts in the query, so 1 is just to test pagination across 3 pages.


Cesar Contreras comments:

Is that the code you have? I don't see the assignment;
$query = new WP_Query( $args );

your code will look like this...

<?php

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

$args = array (
'paged' => $paged,
'post_type' => 'purchase-order',
'posts_per_page' => 1
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) : ?>

<?php while ( $query->have_posts() ) : $query->the_post() ?>

<?=$query->post->post_title?>

<?php endwhile; ?>

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

<?php wp_reset_postdata(); ?>

<?php else : ?>

<p>Sorry, no results matched your criteria</p>

<?php endif; ?>


joshmoto comments:

Yes well spotted, the most important bit

$query = new WP_Query( $args );

Is in my code


Cesar Contreras comments:

only for testing change 'posts_per_page' => 1 to 'posts_per_page' => 2

2021-03-25

Andrea P answers:

I think the problem is here:

<?=$query->post->post_title?>

when you are in the loop you have to access the post from the global $post, not from the query.

but you can just use the wp functions to retrieve post details, which will automatically pull from the currently looped post when you're inside the loop and you don't specifify a post_id as parameter of the function.

so for instance you can just do:


<?php the_title(); ?>


joshmoto comments:

This should not affect pagination. I just this in there as and example.

I am basically after pagination_links to work.


Andrea P comments:

yes sorry I realised I missread your situation...

the method seems fine and I used it many times and in many different scenarios.

it seems a problem with the pretty permalinks.. Something is conflicting with them and so wp doesn't read /page/2/ as paged=2

is /suppliers/ a normal single page? could it be conflicting with anything else such as a custom post type slug or a category with same name (if you don't use the prefix /category/)?

when you access /suppliers/page/2 is the url being rewritten or does it stays like that?

is the /page/2 url working in default wp archives?


joshmoto comments:

Thanks Andre for your response.

/suppliers/ is a page that i've created. And i'm using custom php for this page to run my wp-query.

Ah I do have a custom post type suppliers.

Let me check this is not the confiict


joshmoto comments:

Should be no post type conflict...

My supplier post type is called 'purchase-supplier' and the slug is written too.


'rewrite' => [
'slug' => 'supplier',
'with_front' => false,
'feeds' => false,
'pages' => false,
'ep_mask' => EP_PERMALINK,
],


The archive like page i'm creating is by creating a page called suppliers and using a custom php template with my wp-query.

Everything works fine. The loop is working fine.

The URLs are writing ok...

For example if I run http::mysite.com/suppliers/dhfhb - this returns 404
For example if I run http::mysite.com/suppliers/page/2 - this returns my page with query just on page 1

-----

get_query_var( 'paged' ) just always returns 0.

I've updated permalinks, i'm on multisite too (which shouldn't be issue)

-----

If I manually get the page number from the url and pass this to $paged, the loop outputs the correct data, but pagination_links function still thinks it's on page one.

Let me check if archive template works.


Andrea P comments:

the rewrite slug "supplier" might be also assuming that then your post type main archive page is the plural /supplier/

then wp sometimes goes mad with these things and in some circumstances would give precedence to the single page, for instance, while other times it will get tricked thinking that it should be the post type archive.

though then it would load the post type archive on page/2/ for instance, but instead it loads your code in the single page...

just to exclude the possiblity, maybe try to change the name of the page to something else and see if it works


joshmoto comments:

You were right with your permalink issue assumptions.

I was using a custom class to dynamically route pages.

https://gist.github.com/joshmoto/3f8831f457ca0526dbc80d97a9b43394

Sacked it off and went with normal pages instead. Works fine.

Thanks

2021-03-25

Francisco Javier Carazo Gil answers:

Try changing to:

'paged' => get_query_var( 'page' )


joshmoto comments:

Still no change...

If I do this...


var_dump(get_query_var('page'));


on https://www.mysite.com/suppliers/page/3 for example it outputs this...


string(0) ""