1
votes

So I have created dedicated page template, and on this page I want to list the 3 most recent blog posts, with the usual pagination links to take users to the previous or next 3 posts.

I have the list populated, and the links are appearing, but when I click on either the previous or next links I just get the same 3 posts as before. I can see the URL changes (/blog/page/2) but the posts being shown are always the three most recent ones.

UPDATE. After getting very frustrated I decided to take things back to basics. The following snippet is the only code I had in my template, just to isolate the loop basically, and this STILL didn't fix it. All I get is a singple post on the page, BUT if I manually type /page/2 at the end of the url it takes me to page 2 with a different post showing. However, the only link I see to do with pagination is 'Newer Posts' (and that only appears on page 2). How come 'older' posts isn't showing up?

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'orderby'       => 'post_date',
    'order'         => 'DESC',
    'post_type'     => 'post',
    'post_status'       => 'publish',
    'posts_per_page'    => 1,
    'paged'         => $paged,
);

$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post);
?>

<article>
    <h2 class="fnt25 noBtmMarg"><a href="<?php echo get_permalink(); ?>" title="Read: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <div class="meta fnt18 pMarginBottom">Posted on: <?php the_date(); ?></div>

    <div class="fnt22"><?php print_custom_field('listingDesc'); ?></div>

    <a href="<?php echo get_permalink(); ?>" class="floatRight fnt22" title="Read the full article: <?php the_title(); ?>">Read more...</a>
    <div class="clearfix"></div>

</article>
<?php endforeach; ?>

<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
2
Are you accessing or manipulating wp_query (or get_posts/get_post or the $post object) before this block of code? Also, by any chance are some of the posts you are accessing custom-posts? - zillaofthegods
No this block of code is the first time I'm going near the wp_query or the loop. I do have custom post types, but I do not want them to be included, which is why I am being specific about the 'post' item type. - Kev
Okay, I'm assuming you have more than just 3 total posts (that aren't custom-posts). Under that assumption maybe this suggestion from wordpress? "This function will not work (fail silently) if mysql.trace_mode is enabled in your php.ini. If you can't edit that file, try adding ini_set( 'mysql.trace_mode', 0 ); to your theme's functions.php". Alternatively, what happens when you don't suggest a specific post_type, does it work then (showing both posts and custom-posts)? - zillaofthegods
Indeed I do, I have 6 posts in total so page 2 should fill up. I tried editing the functions.php file with the code, but that made no difference to the outcome. Changing the code to remove the post type stopped it working altogether. The page still rendered but didn't return any posts... Would it be useful to see the full page code up to the point where I copied the bit I already pasted? - Kev
I've found wp.tutsplus.com/tutorials/wordpress-pagination-a-primer to be really helpful. There is an explanation for an alternative options at the bottom called "A Better Solution". Look into that. The only other thing I could imagine being a problem is in your footer. But go through that tutorial, perhaps it mite clarify some things that we aren't seeing. - zillaofthegods

2 Answers

1
votes

Okay. After doing a little bit of research myself, it seems that the query_posts() is not the best for this type of scenario. Use get_posts(), read up on it a bit as well.

A good example (EDIT: better description):

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'orderby'       => 'post_date',
    'order'         => 'DESC',
    'post_type'     => 'post',
    'post_status'       => 'publish',
    'posts_per_page'    => 3,
    'paged'         => $paged,
);

$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post);
?>
0
votes
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination

    $the_query = new WP_Query( array( 
        'post_type' => 'post',
        'paged' => $paged,
        'post_status' => 'publish',
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' => 1) 
    );

    while ( $the_query->have_posts() ) : $the_query->the_post();
        echo '<div>' . get_the_title() . '</div>';
              the_content();
    endwhile;


    echo '<nav>';
    echo  '<div class="nav-previous alignleft">'.get_next_posts_link('Older posts', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
    echo  '<div class="nav-next alignright">'.get_previous_posts_link('Newer posts', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
    echo "</nav>";


    wp_reset_postdata(); // Rest Data

Would you please check above code?