How to use the_posts_navigation for wp_query and get_posts?

WordPress have the_posts_navigation function since 4.1.0. But I don’t know how to use with wp_query or get_posts. the following code is in a template file of page.
wp_query method:

    <?php
    if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
    } else if ( get_query_var('page') ) {
    $paged = get_query_var('page');
    } else {
    $paged = 1;
    }

    $get_posts=new wp_query('post_type=case&posts_per_page=2&paged='.$paged);

    while($get_posts->have_posts()):$get_posts->the_post();
    the_title();
    endwhile;

    the_posts_pagination( array(
        'prev_text'          => __( 'Previous page', 'cm' ),
        'next_text'          => __( 'Next page', 'cm' ),
        'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'cm' ) . ' </span>',
    ) );
    ?>

get_posts method:

    <?
    while(have_posts()):the_post();
    if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
        } else if ( get_query_var('page') ) {
        $paged = get_query_var('page');
        } else {
        $paged = 1;
    }

    $case_posts=get_posts('post_type=case&posts_per_page=2&paged='.$paged);
    echo '<pre>';
    print_r($case_posts);
    echo '</pre>';
    foreach($case_posts as $case_post){
        echo $case_post->post_title;
    }
    endwhile;
    // Previous/next page navigation.
    the_posts_pagination( array(
        'prev_text'          => __( 'Previous page', 'cm' ),
        'next_text'          => __( 'Next page', 'cm' ),
        'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'cm' ) . ' </span>',
    ) );
    ?>

They don’t work and display pagination, but entering http://127.0.0.1/gdboer/?page_id=74&page=2 manually in address bar, it works.
Who can help me, thank you so much!

3 s
3

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

Leave a Comment