Skip to content
Advertisement

WordPress pagination not working for get_posts() function?

I have make one Template and assign a template to testimonial page. All the below code is placed inside my template file.

My problem is that Nextand Prev links are not even displaying on Testimonial page. I want to use only get_posts query to retrieve data.

How can I display pagination for get_posts?

global $post;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 0;

$postsPerPage = 2;
$postOffset = $paged * $postsPerPage;

$args_testimonial = array(
'post_type' => 'testimonial',
'offset'          => $postOffset,
'posts_per_page'  => $postsPerPage,
'orderby'=> 'title',
'order'=>'DESC'
);

$data_testimonial = get_posts( $args_testimonial ); 



foreach ( $data_testimonial as $post_testimonial ) : setup_postdata( $post_testimonial ); 

                $testimonee_title = get_the_title($post_testimonial->ID);
                $testimonee_post = get_post($post_testimonial->ID);
                $testimonee_content = $testimonee_post->post_content; 
                $testimonee_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_testimonial->ID ), 'single-post-thumbnail' );




       if ( $testimonee_title || $testimonee_content || $testimonee_image ) { ?>
        <div class="single-testimonial">
        <?php if ( $testimonee_content ) { ?>
            <div class="testimonial-content">
                <p>Dear Gary’s Carpet Cleaning</p>
                <p><?php echo $testimonee_content; ?></p>
            </div>
            <?php } 

            if ( $testimonee_title || $testimonee_image ) { ?>
            <div class="testimonial-person">
                <div class="testimonial-image">
                <?php if ( $testimonee_image ) { ?>
                    <img src="<?php echo $testimonee_image[0]; ?>" alt=" "/>
                <?php } else { ?>
                <img src="<?php echo get_template_directory_uri(); ?>/assets/images/testimonial-dummy-image.png" alt=" "/>
                <?php } ?>
                </div>
                    <?php if( $testimonee_title ) { ?>
                <div class="testimonial-name">
                    <p><strong>-<?php echo $testimonee_title; ?></strong></p>
                </div>
                <?php } ?>
            </div>
            <?php } ?>

        </div>

        <?php } endforeach; 
        next_posts_link( 'Prev'); 
        previous_posts_link('Next'); 
        wp_reset_postdata();

Advertisement

Answer

Please don’t use get_posts if you need paginated queries.

I think the easiest and most appropriate here is to make use of WP_Query to construct your custom query I do think that next_posts_link() and previous_posts_link() is better to use with WP_Query

global $post;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 0;

$postsPerPage = 2;
$postOffset = $paged * $postsPerPage;

$args_testimonial = array(
'post_type' => 'testimonial',
'offset'          => $postOffset,
'posts_per_page'  => $postsPerPage,
'orderby'=> 'title',
'order'=>'DESC'
);
$postslist = new WP_Query( $args_testimonial );

    if ( $postslist->have_posts() ) :
        while ( $postslist->have_posts() ) : $postslist->the_post(); 
            the_title();
        endwhile;
        next_posts_link( 'Older Entries', $postslist->max_num_pages );
        previous_posts_link( 'Next Entries &raquo;' ); 
        wp_reset_postdata();
        endif;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement