Support » Plugins » Trying to display most recent single post, with pagination

  • Hello all,

    I’m sure this has been answered before, but the results are scattered to the winds, and some of them just plain don’t work.

    I’m simply trying to use query_posts to display the single most recent post on my index page. However, I cannot find a solution that allows the next/prev links to remain functional.

    I’ve tried many solutions over the past two days. I though I found my solution here:

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

    But this does not work. It correctly limits the front page to one post however, when I click on the previous post link it shows me my 404.php page.

    My page is if you’d like to see what I mean.

    Does anyone see what I’m missing, or is there something else I should be trying?


Viewing 10 replies - 1 through 10 (of 10 total)
  • I think you want posts_per_page=1 and not showposts. Take a look at Example 3 on the query_posts Codex page.

    Thanks for the reply!

    If that has any effect I’m not seeing it. I’m now using this:

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

    And as you can see, I’m still getting broken links. Any other ideas?

    Thanks again.

    I took a further look at the “Example 3” you referenced, and explicitly used it. It also causes the pagination problem. I’ve tried so many “solutions” I don’t know what to try next. If anyone thinks it’ll help, I’ll post my whole index.php


    I guess the next question is what is making the next/previous links?

    Okay, I’ve narrowed this down.

    My blog is still in testing so I only have 3 posts. In the “reading” settings I have “Show at most 10 posts”. I think what’s happening is this:

    WordPress is getting a request for page/2 when it thinks that all the posts should fit on one page – so, it’s giving a 404.

    If I change to “Show at most 2 posts”. It works…until you try to view the 3rd post. So, wordpress expects “page/2” now, but not “page/3” and it gives a 404.

    So what is the solution here? It’s like I need to catch it before it looks to see if there should be a “page/whatever” and decide, based on the number of posts, what to show.

    Thanks again,

    You are redirecting from a home page that handles the single post to a different page that handles the rest of the posts, right? If you aren’t sure then do this: for each of your template pages put echo 'whateverthepagenameis'; right at the top. You can also put echo $wp_query->request; in there to echo the query string, or print_r($wp_query); if you want a truly tremendous amount of data.

    I’m not sure what you’re asking. All the code I’ve posted is from the top of my index.php page.

    I just want it to always show one post, with previous and next buttons to navigate through posts. I know it would do this if I set the reading options to “show at most 1 post” but that would affect the whole site, which I do not want.

    Thanks for the replies,

    Okay, I looked at what you wrote again.

    No, I’m not trying to redirect to a different page to handle the rest of the posts. I’m trying to do it all on the index. Is this wrong?


    Is this wrong?

    I don’t know. It might be. I haven’t figured this out yet either. 🙂

    Just for fun, throw this in your functions file.

    function filter_limit($qstr) {
      global $wpdb, $wp_query;
      if (!is_home()) {
        $limstart = (($matches[1]-1) >=0) ? $matches[1]-1 : 0;
        $limend = trim($matches[2]);
        $qstr = preg_replace('#\sLIMIT\s(.),.*#'," LIMIT $limstart,$limend",$qstr);
      return $qstr;
    add_filter('posts_request', 'filter_limit');

    I’m not sure this, specifically, will work, but I think you’ll have to do something like that to do what you want. WP uses the page number to calculate the query LIMIT start and end figures. What you see above is rough and inflexible but it illustrates a way to manipulate the query LIMIT, which it looks like you’ll need to do.

    Thank you again for the help. I haven’t had time to try this yet but I’ll post when I do.

    In my opinion, something like this needs to be common information. I’ve seen other people post this problem, but there is either no solution posted or one that doesn’t work for me. If I can get this to work, I’ll try to distill the process to something manageable and slap it into the codex.

    Thanks again,

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Trying to display most recent single post, with pagination’ is closed to new replies.