9

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!

1
  • 1
    Your question is mislead, since your code use the_posts_pagination, it's different with your title question the_posts_navigation.
    – Jevuska
    Apr 2, 2016 at 20:02

3 Answers 3

10

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

4
  • Thank you for your reply. Now I know the function can't to work with custom queries. By the way, you don't like nullifying the main query. But which method did you used? Jan 16, 2015 at 0:31
  • I don't make use of the native function, I wrote my own pagination function which I use. You can check it out here Jan 16, 2015 at 4:14
  • If I use permalink like this 127.0.0.1/gdboer/?post_type=case it didn't work. use permalink 127.0.0.1/gdboer/?page_id=74 is OK. Jan 17, 2015 at 2:14
  • I will play around with this in the coming week. Caught up in a few other things at this stage. Many thanks for your reply. This really helps me a lot. Jan 17, 2015 at 6:24
9

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') ?>
2
  • 1
    Definitely an idea, the only part that I don't really like is changing the main query object. It is kind of hacky and a bug that is going to give you nightmares because you do not reset the main query once you are done. Just keep that in mind. That is why globalisation is such an evil thing, anyone can change the values. Remember, many plugins rely on globals, specially $wp_query and $post Apr 25, 2015 at 5:33
  • 1
    Many thanks. $GLOBALS['wp_query']->max_num_pages was the missing bit for me. Not sure there is a better way.
    – Space
    Aug 10, 2017 at 23:17
3

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

2
  • thank you for your reply. I know paginate_links is work for me. but I want to use new function the_posts_navigation. I carefully watched get_the_posts_navigation source code, and refer to the theme of twentyfifteen. Jan 15, 2015 at 8:16
  • i couldn't get get_the_posts_pagination() to work in my ajax query, but paginate_links() works fine. thanks for that.
    – honk31
    Jan 14, 2020 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.