Forum

Thread tagged as: Question, Problem, Blog

Issue with previous link for blog posts

Is anyone else getting an issue with the previous link on their blogs where it links to the current post and not the previous one? The next link works absolutely fine but I cannot see what's up with previous link.

I have Perch 2.8.7, the is Blog App 4.6 and PHP is 5.6.7.

The HTML templates for $next and $prev just contain an <a> for the links.

<?php
            $result = perch_blog_custom(array(
                'filter' => 'postSlug',
                'match' => 'eq',
                'value' => perch_get('s'),
                'skip-template' => true,
                'return-html'   => true,
            ));

            $next = perch_blog_custom(array(
                'template'   => 'blog/post_next.html',
                'filter'     => 'postDateTime',
                'match'      => 'gt',
                'value'      => $result[0]['postDateTime'],
                'sort'       => 'postDateTime',
                'sort-order' => 'ASC',
                'count'      => 1,
                'skip-template' => true,
                'return-html'   => true,
            ));

            $prev = perch_blog_custom(array(
                'template'   => 'blog/post_prev.html',
                'filter'     => 'postDateTime',
                'match'      => 'lt',
                'value'      => $result[0]['postDateTime'],
                'sort'       => 'postDateTime',
                'sort-order' => 'DESC',
                'count'      => 1,
                'skip-template' => true,
                'return-html'   => true,
            ));

            echo $result['html'];
        ?>

        <div>
            <h3>What to read next</h3>
            <nav>
                <?php
                    echo $next['html'];
                    echo $prev['html'];
                ?>
            </nav>
        </div>
Alex Blackman

Alex Blackman 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

That looks fine. What does debug output?

For context there are three articles: one, two and three. Article three is the newest and article one is the oldest:

  • Article three published 31 May 2015
  • Article two published 22 May 2015
  • Article one published 17 May 2015

    The debug below is from article two. The result I get is the next post is article three (correct), but the previous post is article two when it should be article one.

Debug Message
SELECT * FROM perch2_blog_posts WHERE postStatus='Published' AND postDateTime<='2015-05-31 13:32:00' AND postSlug='article-two'
SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
SELECT * FROM perch2_blog_sections ORDER BY sectionTitle ASC
Using template: /templates/blog/post.html

SELECT SQL_CALC_FOUND_ROWS DISTINCT tbl.* FROM ( SELECT idx.itemID, main.*, idx2.indexValue as sortval FROM perch2_blog_index idx JOIN perch2_blog_posts main ON idx.itemID=main.postID AND idx.itemKey='postID' JOIN perch2_blog_index idx2 ON idx.itemID=idx2.itemID AND idx.itemKey='postID' AND idx2.indexKey='_id' WHERE 1=1 AND ((idx.indexKey='postSlug' AND idx.indexValue='article-two')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2015-05-31 13:32:00' ) GROUP BY itemID ORDER BY sortval ASC LIMIT 0, 10
SELECT FOUND_ROWS() AS `count`
Using template: /templates/blog/post.html

SELECT SQL_CALC_FOUND_ROWS DISTINCT tbl.* FROM ( SELECT idx.itemID, main.*, idx2.indexValue as sortval FROM perch2_blog_index idx JOIN perch2_blog_posts main ON idx.itemID=main.postID AND idx.itemKey='postID' JOIN perch2_blog_index idx2 ON idx.itemID=idx2.itemID AND idx.itemKey='postID' AND idx2.indexKey='postDateTime' WHERE 1=1 AND ((idx.indexKey='postDateTime' AND idx.indexValue > '2015-05-22 00:00:00')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2015-05-31 13:32:00' ) GROUP BY itemID ORDER BY sortval ASC LIMIT 0, 1
SELECT FOUND_ROWS() AS `count`
Using template: /templates/blog/post_next.html

SELECT SQL_CALC_FOUND_ROWS DISTINCT tbl.* FROM ( SELECT idx.itemID, main.*, idx2.indexValue as sortval FROM perch2_blog_index idx JOIN perch2_blog_posts main ON idx.itemID=main.postID AND idx.itemKey='postID' JOIN perch2_blog_index idx2 ON idx.itemID=idx2.itemID AND idx.itemKey='postID' AND idx2.indexKey='postDateTime' WHERE 1=1 AND ((idx.indexKey='postDateTime' AND idx.indexValue < '2015-05-22 00:00:00')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2015-05-31 13:32:00' ) GROUP BY itemID ORDER BY sortval DESC LIMIT 0, 1
SELECT FOUND_ROWS() AS `count`
Using template: /templates/blog/post_prev.html
Drew McLellan

Drew McLellan 2638 points
Perch Support

That really looks like it should work.

If you run the queries directly, do you get sane results back?