Support » Developing with WordPress » Pagination Not Working on `WP_Query` Archive Page

  • pk-71

    (@pauljohnknight)


    Hi,

    I have an archive page that pulls 2 different custom post types into an archive using WP_Query(). Because there is a home page to the site which shows 9 of the custom post type posts, I offset the archive page by 9 posts.

    The code for this is:

    $homePageArticles = new WP_Query(array(
            'posts_per_page' => 18,
            'offset' => 9,
            'post_type'=> array('articles', 'featured') // pulls in 2 post-types
        ));
    
        while(  $homePageArticles->have_posts()){
                $homePageArticles->the_post(); ?>
    
                // HTML
    
    <?php } ?>

    However on this archive page the <?php echo paginate_links();?> function to show the pagination pages doesn’t work.

    I can click through the numbers or use the next and previous arrows, but it just shows the same posts on each page.

    The pagination code I’m using is:

    <p>
        <?php echo paginate_links(array(
            'prev_text' => 'NEWER',
            'next_text' => 'OLDER',
            'mid_size' => 0
        ));?>
    </p>

    On normal custom archive pages on the site (which don’t use WP_Query();) the pagination works fine.

    Any help or assistance would be amazing.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Check out the examples of proper use of paginate_links() in the user notes at the bottom of the docs page:
    https://developer.wordpress.org/reference/functions/paginate_links/

    Thread Starter pk-71

    (@pauljohnknight)

    I’ve looked at that already and can’t seem to work out how to use it with a WP_Query() that is using offset. The reason it isn’t working seems to be two-fold: WP_query() affects pagination out of the box, as does using offset. I’m at a bit of a loss where to start because PHP is all new to me.

    Instead of using new WP_Query, use the pre_get_posts filter to affect the main query. https://developer.wordpress.org/reference/hooks/pre_get_posts/

    Moderator bcworkz

    (@bcworkz)

    Using “offset” breaks normal WP pagination by design. When you use it you need to manage all pagination yourself. If this query is the main query for the page, Joy’s suggestion is spot on. Using pre_get_posts allows typical WP pagination functions to work normally. However, do not set “offset” there. Setting “posts_per_page” and “paged” will do the job, along with setting “post_type”.

    Thread Starter pk-71

    (@pauljohnknight)

    How do I know what is the main query and what isn’t? This is all new to me and when I read articles it’s so confusing. I have the above WP_Query() (in the original question) and the paginate_links() function and the rest of the page is HTML. The WP_Query() is pulled into an archive page with the URL example.com/articles (there is a custom post type of articles) and as you can see on the original post this archive page actually pulls in two custom post types with because of the 'post_type'=> array('articles', 'featured') line in the WP_Query()

    But I have no idea how main and secondary queries work or how to approach this? The documentation on this seems very scant.

    WordPress does the main query, based on the page address (and its parameters).
    Any time your code does a query, it’s not the main query.
    There is also a function that you can call when your code within a filter needs to distinguish the main query and other queries.
    https://developer.wordpress.org/reference/functions/is_main_query/

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Pagination Not Working on `WP_Query` Archive Page’ is closed to new replies.