Forum

Thread tagged as: Problem, Blog

Next link on blog posts

I'm using next/previous links on blog posts. Previous link always works fine.

But next link usually shows a link to the current post. Weirdly, on 3 posts it shows correctly a link to the next post. I've got 12 posts at the moment, so the next link is correct on 3 and incorrect on 9.

Here's my code (based on Drew's suggestion here: https://forum.grabaperch.com/forum/01-10-2015-nextprev-buttons-still-not-working-correctly):

                      <?php
                      // Get the current post and store it in $result:
                      $result = perch_blog_custom(array(
                          'filter' => 'postSlug',
                          'match' => 'eq',
                          'value' => perch_get('s'),
                          'skip-template' => true,
                          'return-html'   => true,
                          )); 
                      // Show the post
                      echo $result['html'];
                      ?>

                      <?php
                      // To show the previous link (ie a more recent post)
                      PerchSystem::set_var('is_prev', true);
                      perch_blog_custom(array(
                          'template'   => 'blog/post_prev.html',
                          'filter'     => 'postDateTime',
                          'match'      => 'gt',
                          'value'      => $result[0]['postDateTime'],
                          'sort'       => 'postDateTime',
                          'sort-order' => 'ASC',
                          'count'      => 1,
                          ));
                      // To show the next link (ie an  older post)
                      PerchSystem::set_var('is_next', true);
                      perch_blog_custom(array(
                          'template'   => 'blog/post_next.html',
                          'filter'     => 'postDateTime',
                          'match'      => 'lt',
                          'value'      => $result[0]['postDateTime'],
                          'sort'       => 'postDateTime',
                          'sort-order' => 'DESC',
                          'count'      => 1,
                          ));
                      ?>

Diagnostics:

    Perch: 2.8.24, PHP: 5.5.30, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: 15d5c781cfcad91193dceae1d2cdd127674ddb3e $, with PDO
    Server OS: Linux, cgi-fcgi
    Installed apps: content (2.8.24), assets (2.8.24), categories (2.8.24), perch_blog (5.0), perch_forms (1.8.3)
    App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_forms', 'perch_blog', ); ?>
    PERCH_LOGINPATH: /cms
    PERCH_PATH: /home/blizzietestco/public_html/cms
    PERCH_CORE: /home/blizzietestco/public_html/cms/core
    PERCH_RESFILEPATH: /home/blizzietestco/public_html/cms/resources
    Image manipulation: GD
    PHP limits: Max upload 250M, Max POST 250M, Memory: 128M, Total max file upload: 128M
    F1: dc1fef2ad0fcd9f943c02ebb43d85dbc
    Resource folder writeable: Yes
    DOCUMENT_ROOT: /home/blizzietestco/public_html
    HTTP_HOST: buzylizzie.test-singlepage.co.uk
    REQUEST_URI: /cms/core/settings/diagnostics/
    SCRIPT_NAME: /cms/core/settings/diagnostics/index.php

Debug code from one of the post pages that shows an incorrect next link:

Debug Message
SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/news/post.php' OR regionPage='*' ORDER BY regionPage DESC
SELECT * FROM perch2_pages WHERE pagePath='/news/post.php' LIMIT 1
Using template: /templates/pages/attributes/default.html
Using template: /templates/pages/attributes/default.html
Using template: /templates/pages/attributes/default.html
Using template: /templates/pages/attributes/default.html
SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 ORDER BY pageTreePosition ASC
SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/news/post.php' LIMIT 1
SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-006-001', '000-006', '000') ORDER BY pageTreePosition DESC
SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 ORDER BY pageTreePosition ASC
SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/news/post.php' LIMIT 1
SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-006-001', '000-006', '000') ORDER BY pageTreePosition DESC
Using template: /templates/navigation/item.html
SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 ORDER BY pageTreePosition ASC
SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/news/post.php' LIMIT 1
SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-006-001', '000-006', '000') ORDER BY pageTreePosition DESC
Using template: /templates/navigation/mobile_menu.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='2015-01-26-cumbria-life-home-garden-show-2015')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue ) as tbl WHERE (postStatus='Published' AND postDateTime<='2016-03-01 09:37:00' ) GROUP BY itemID, sortval ORDER BY sortval ASC LIMIT 0, 10
SELECT FOUND_ROWS() AS `count`
SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
Using template: /templates/blog/post.html
SELECT * FROM perch2_blog_sections ORDER BY sectionTitle ASC
SELECT * FROM perch2_blogs ORDER BY blogTitle ASC
Fetching from cache: perch_blog_post_categoriesc9f9067c02f51ff3cab166f6b9360dc1
Cache file not found: perch_blog_post_categoriesc9f9067c02f51ff3cab166f6b9360dc1
SELECT * FROM perch2_blog_posts WHERE postStatus='Published' AND postDateTime<='2016-03-01 09:37:00' AND postSlug='2015-01-26-cumbria-life-home-garden-show-2015'
Using template: /addons/apps/perch_blog/templates/blog/post_category_link.html
Fetching from cache: perch_blog_post_tags32f66ff88140b707daaa96ebf8bd684c
Cache file not found: perch_blog_post_tags32f66ff88140b707daaa96ebf8bd684c
SELECT t.* FROM perch2_blog_tags t, perch2_blog_posts_to_tags p2t WHERE t.tagID=p2t.tagID AND p2t.postID='3'
Using template: /addons/apps/perch_blog/templates/blog/post_tag_link.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-01-26 00:00:00')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue ) as tbl WHERE (postStatus='Published' AND postDateTime<='2016-03-01 09:37:00' ) GROUP BY itemID, sortval ORDER BY sortval ASC LIMIT 0, 1
SELECT FOUND_ROWS() AS `count`
Using template: /templates/blog/post_prev.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-01-26 00:00:00')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue ) as tbl WHERE (postStatus='Published' AND postDateTime<='2016-03-01 09:37:00' ) GROUP BY itemID, sortval ORDER BY sortval DESC LIMIT 0, 1
SELECT FOUND_ROWS() AS `count`
Using template: /templates/blog/post_next.html
Fetching from cache: perch_blog_categories589c7a634eb12f2ec225cfedc7dc78ae
Cache file not found: perch_blog_categories589c7a634eb12f2ec225cfedc7dc78ae
SELECT setID FROM perch2_category_sets WHERE setSlug='blog' LIMIT 1
SELECT main.* , COALESCE(cc.countValue,0) AS `count.blog.post` FROM perch2_categories main LEFT OUTER JOIN perch2_category_counts cc ON cc.catID=main.catID WHERE 1=1 AND setID=1 AND (cc.countType='blog.post' OR cc.countType IS NULL) AND cc.countValue > 0 ORDER BY catTreePosition ASC
Using template: /addons/apps/perch_blog/templates/blog/category_link.html
Fetching from cache: perch_blog_tagse0b2ff59ed95ac2a35516ee8013c2dd2
Fetching from cache: perch_blog_date_archive_years06b1ec41355f83614225087b92313523
Fetching from cache: perch_blog_date_archive_monthsf2e7d49784704a5b24102a78c349d57c
SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 ORDER BY pageTreePosition ASC
SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/news/post.php' LIMIT 1
SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-006-001', '000-006', '000') ORDER BY pageTreePosition DESC
Using template: /templates/navigation/footer_menu.html

I've tried increasing count to 2 for the next links to see what order it is trying to sort the posts into: on the posts where the first link is incorrectly to the current article, the second link is to the next article (so this is the one I really want to link to). So it does seem to be fifltering and sorting posts correctly; it's just sometimes starting in the wrong place.

I've tried 'start' => 2, but this seems to have no effect - the next links are unchanged.

I'm using similar code on perch_content_custom in a different part of the same site, and the next/previous links there are working fine.

Mark Melling

Mark Melling 0 points

  • 5 years ago

It's getting a little less weird. I can't reproduce the 3 correct links now.

So all "next" links are now consistently pointing to the current post instead of to the next post.

I'd be really grateful if anyone can point me in the right direction. I'm using fairly standard code that I'm sure is working on other sites.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Do your posts have distinct timestamps?

They didn't when I first posted. Then I noticed that 2 posts had the same date and, because I'd used time="false" in my post.html template, they both had the same time.

So I then changed the date of one of those posts. All posts now have unique dates. But all my "next" links are still pointing to the current post.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Do they all have times now?

Inspecting the database, all the posts have time 00:00:00 and the dates are all different.

I've just tested the same next/prev link approach (same post.php code as shown above) on another site I'm building and am getting weird results. This site has 13 posts, all with different dates. Again I'm using time="false" in my post.html. Here only 3 of the "next" links incorrectly point to the current post; and 10 of them are correctly pointing to the next post.

Here's my post.html template from the first site:

<article class="h-entry">
    <h1>
        <perch:blog id="postTitle" type="text" label="Title" required="true" size="xxl autowidth" order="1" />
    </h1>
    <p class="meta">
        <time class="dt-published" datetime="<perch:blog id="postDateTime" type="date" time="false" label="Date" format="Y-m-d H:i:s" divider-before="Publishing" />">
            <perch:blog id="postDateTime" type="date" time="false" format="%d %B %Y" order="2" /> 
        </time>
        <perch:categories id="categories" set="blog" label="Categories" display-as="checkboxes">
            <a href="archive.php?cat=<perch:category id="catSlug" type="slug" />" class="p-category">
                <perch:category id="catTitle" type="text" />
            </a>
        </perch:categories>
    </p>
    <perch:if exists="image">
        <div class="main_photo">
            <img src="<perch:blog id="image" type="image" width="960" label="Image" disable-asset-panel="true" order="3" />" alt="<perch:blog id="postTitle" />" />
              <p class="caption">
                <perch:blog id="imagecaption" type="text" size="xxl autowidth" label="Image caption" order="4" />
              </p>
        </div>
    </perch:if>
    <div class="description e-content">
        <perch:blog id="postDescHTML" type="textarea" label="Post" order="5" html="true" editor="redactor" editor-config="content" size="xxl autowidth" required="true" />
    </div>
</article>
<perch:blog id="excerpt" type="textarea" label="Excerpt" html="true" editor="redactor" editor-config="minimal" order="6" suppress="true" size="s" help="Max: 30 words. If left blank, the system will automatically display the first 30 words from the Post as an excerpt" />
<perch:blog id="image" type="image" width="350" height="350" crop="true" suppress="true" />
<perch:blog id="image" type="image" width="50" height="50" crop="true" suppress="true" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you remove the count from the previous and next calls, what do you get back?

Hi - thanks for persevering with this!

After removing both counts, I get:

  • a list of previous links (up to 10 links), starting with the previous post - ie all looking fine

  • and a list of next links (up to 10 links), starting incorrectly with the current post. All other links in the next list look fine - no posts missing and they're in the right order.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't understand what's happening there, but do you get the right result by offsetting by one?

'count' => 1,
'start' => 2,
'paginate' => false,

This fixes the site where all the next links were going to the current post. I had tried this before, without the 'paginate' => false, and it had no effect at all on the links. But this time it worked.

However, on the second site, where some of the "next" links work correctly, this moves them all forward by one post. So the ones that were broken now work. But the ones that were working, are now broken.

I'm really grateful for your help with this. I think I'm going to work without the next/prev links for now. I feel like I'll wake up one morning with a flash of inspiration and realise I've been doing something stupid!

Thanks again Drew.

Drew McLellan

Drew McLellan 2638 points
Perch Support

If your other site is working, why are you changing it?

Well I could leave these next/prev links on the one site, where they now work. But they're only working because we've used 'start' => 2, which we shouldn't really need. I'd feel happier if I understood why they're working this way. And, since I don't really understand what's going on, I think I'd prefer to leave them off!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, that's fine. Can you tell me what's different between the two?

When I first reported on the 2 sites, the only difference I can think of was that one was using clean urls for the blog and the other wasn't. But today I've switched the second site to clean urls; I've also added a new post. This second site, which earlier today had 3 broken "next" links, now has 4. So clean urls hasn't fixed the problem.

Both sites are using Perch 2.8.24 and Blog 5.0. Both are sitting on the same VPS - so the same server environment shown in diagnostics above.

It's not clear to me that this is a problem with Perch - it almost seems too weird for that! So I'm not asking you to spend any more time on this - I'm planning to work around it for now. If I find what's causing it in the next few days, I'll post back here.