Wordpress - How to use the_posts_navigation for wp_query and get_posts?

the_posts_navigation() is simply a wrapper function for get_the_posts_navigation() which issimply a wrapper function for paginate_links. The first two functions uses the the same exact parameters that is being used by paginate_links and actually passes it to the latter function as well

get_the_posts_navigation() and the_posts_navigation() is good new functions as it eliminates a lot of custom coding and it is more user friendly for new unexperienced users who would like numbered pagination links

The only flaw in this get_the_posts_navigation() is that the developers went and wrapped the paginate_links function in a conditional statement that states that if the main query ($wp_query) has less than 1 page, (remember, the first page is 0 and the second page is 2), don't show the links. This is problematic for custom queries on page templates. Pages will always have just one page, so these functions does not work with custom queries

The only true workaround if you have to use the_posts_navigation(), is to make use of @ChipBennet answer in this post. I really don't like nullifying the main query (quite hacky, in my opinion this is just like using query_posts) but I can't see any other solution to make get_the_posts_navigation() to work with custom queries


I have a custom template and I struggled hours to show the pagination component. here what's worked for me.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'posts_per_page' => 3,
    'orderby' => 'menu_order',
    'order'=> 'ASC',
    'paged'=>$paged,
    'post_type' => 'projects'
    );

$projects = new WP_Query($args);

            <!-- working example of pagination with numbers -->
            ...<?php endwhile;?>
                <?php 
                $GLOBALS['wp_query']->max_num_pages = $projects->max_num_pages;
                the_posts_pagination( array(
                   'mid_size' => 1,
                   'prev_text' => __( 'Back', 'green' ),
                   'next_text' => __( 'Onward', 'green' ),
                   'screen_reader_text' => __( 'Posts navigation' )
                ) ); ?>
             OR
             <!-- working example of pagination without numbers -->
             ...<?php endwhile;?>   
             <?php next_posts_link(  'next', $projects->max_num_pages ); ?>
             <?php previous_posts_link('prev') ?>

This function uses the get_the_posts_pagination() which uses the GLOBAL wp_query to setup the paginate_links() function, so I believe that doesn't work for get_posts.

Try use the function paginate_links() by itself or the function posts_nav_link()

PS: Make sure you use wp_reset_query()