Horje
WP Query pagination outputs, but page links do not change the query. - Wordpress Solution
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?

Solution - 1

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/


Solution - 2

Try removing the ], 'posts_per_page' => 1 ]; or changing it to -1


Solution - 3

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


Solution - 4

Try changing to: 'paged' => get_query_var( 'page' )





Wordpress

Related
Drop Down Menu Arrow Missing - Wordpress Solution Drop Down Menu Arrow Missing - Wordpress Solution
How to get wordpress multisite working with subdomains in docker-compose.yml - Wordpress Solution How to get wordpress multisite working with subdomains in docker-compose.yml - Wordpress Solution
Update post meta in another post after draft -> publish transition - Wordpress Solution Update post meta in another post after draft -> publish transition - Wordpress Solution
Display custom post group by taxonomy current term and sub-term - Wordpress Solution Display custom post group by taxonomy current term and sub-term - Wordpress Solution
custom menu html structure - Wordpress Solution custom menu html structure - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
43