We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question, Problem, Blog

Related Blog Posts by Category

I am trying to get a list of related posts under my article that is ordered by Category and doesnt display the current post.

I seen another forum post on this and copied the code in that thread but it isnt working for me and I can't work out what I have done differently for it not to be showing

Here is my code

<?php

        $categories = perch_blog_post_categories(perch_get('s'), array(
           'skip-template'=>true, ));

        if (count($categories)) {
            $cat_slugs = array();
            foreach($categories as $cat) {
            $cat_slugs[] = $cat['catSlug'];
            }

         perch_blog_custom(array(
             'filter' => 'postSlug',
             'match' => 'neq',
             'value' => perch_get('s'),
             'category' => $cat_slugs,
             'sort-order'=> 'RAND',
             'count'=> 3,
             'template'=> 'blog/related_posts.html',
             'section'=> 'blog'
          ));
        }

    ?>

The template is the following

<perch:before>
    <div class="section  section--nobottom-minus  clearfix">
        <h2 class="lone-title  h2  secondary  align-left">Related News & Media</h2>
</perch:before>

        <div class="related-article  <perch:every nth-child="odd">left<perch:else />right</perch:every>">
            <div class="article-image">
                <a href="<perch:blog id="postURL" />"><img src="<perch:blog id="image" type="image" width="194" height="104" crop="true" />" alt="<perch:blog id="postTitle" />" /></a>
            </div>
            <div class="article-info">
                <h3><a href="<perch:blog id="postURL" />"><perch:blog id="postTitle" /></a></h3>

                <time datetime="<perch:blog id="postDateTime" format="Y-m-d" />" pubdate="pubdate" class="article-info--date"><perch:blog id="postDateTime" format="d F Y" /></time>

                <perch:blog id="excerpt" type="textarea" textile="true" />
            </div>
        </div>

<perch:after>
    </div>
</perch:after>

Anybody got any ideas why this is not outputting the related posts?

P.S Perch is up to date, Blog is up to date Perch Runway: 2.8.4, PHP: 5.4.10, MySQL: 5.5.29, with PDO Server OS: Darwin, apache2handler Installed apps: content (2.8.4), assets (2.8.4), categories (2.8.4), perch_blog (4.6)

Fishtank Creative

Fishtank Creative 2 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Does it make a difference if you use

'category-match' => 'any'

Unfortunately not.

I have turned on PERCH_DEBUG and I get this message, if that helps

Time    Δ  Debug Message
0.0534  0   SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate FROM perch2_pages p LEFT JOIN perch2_page_routes pr ON p.pageID=pr.pageID ORDER BY pr.routeOrder ASC
0.1332  0.0798  Matched route: news/[blogslug:s]
0.1334  0.0001  Using master page: /templates/pages/blog/post.php
0.1334  0   Page arguments:
Array
(
    [0] => /news/2014-12-09-warriors-get-ready-for-world-club-series
    [s] => 2014-12-09-warriors-get-ready-for-world-club-series
    [1] => 2014-12-09-warriors-get-ready-for-world-club-series
)
Drew McLellan

Drew McLellan 2638 points
Perch Support

Is that all you get? I can't see it querying any posts at all.

Sorry there is more here it is in full

Time    Δ  Debug Message
0.0635  0   SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate FROM perch2_pages p LEFT JOIN perch2_page_routes pr ON p.pageID=pr.pageID ORDER BY pr.routeOrder ASC
0.3972  0.3337  Matched route: news/[blogslug:s]
0.3974  0.0002  Using master page: /templates/pages/blog/post.php
0.3975  0.0001  Page arguments:
Array
(
    [0] => /news/2014-12-09-warriors-get-ready-for-world-club-series
    [s] => 2014-12-09-warriors-get-ready-for-world-club-series
    [1] => 2014-12-09-warriors-get-ready-for-world-club-series
)
0.3983  0.0009  SELECT * FROM perch2_blog_posts WHERE postStatus='Published' AND postDateTime<='2015-03-30 17:20:00' AND postSlug='2014-12-09-warriors-get-ready-for-world-club-series'
0.4486  0.0503  SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
0.4633  0.0148  SELECT * FROM perch2_blog_sections ORDER BY sectionTitle ASC
0.4801  0.0168  Using template: /templates/blog/post.html
0.4811  0.0009  SELECT * FROM perch2_pages WHERE pagePath='/news/post' LIMIT 1
0.4996  0.0185  Using template: /templates/pages/attributes/seo.html
0.5002  0.0006  SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageDepth >=0 AND pageDepth<=2 ORDER BY pageTreePosition ASC
0.5484  0.0482  SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/news/post' LIMIT 1
0.563   0.0146  SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-003-001', '000-003', '000')
0.5804  0.0174  Using template: /templates/navigation/main_nav.html
0.6076  0.0273  SELECT setID FROM perch2_category_sets WHERE setSlug='blog' LIMIT 1
0.6255  0.0179  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='4' AND (cc.countType='blog.post' OR cc.countType IS NULL) AND cc.countValue > 0 ORDER BY catTreePosition ASC
0.6403  0.0147  Using template: /templates/blog/category_page_nav.html
0.6448  0.0045  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='2014-12-09-warriors-get-ready-for-world-club-series')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2015-03-30 17:20:00' ) GROUP BY itemID ORDER BY sortval ASC LIMIT 0, 10
0.6954  0.0506  SELECT FOUND_ROWS() AS `count`
0.7115  0.0161  Using template: /templates/blog/post.html
0.7163  0.0048  
Array
(
    [0] => categories
)
0.7164  0.0001  SELECT catID, catPath FROM perch2_categories
0.7357  0.0193  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 < '2014-12-09 11:57:00')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2015-03-30 17:20:00' ) GROUP BY itemID ORDER BY sortval DESC LIMIT 0, 1
0.7557  0.02    SELECT FOUND_ROWS() AS `count`
0.7715  0.0158  Using template: /templates/blog/post_page_prev.html
0.7728  0.0012  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 > '2014-12-09 11:57:00')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2015-03-30 17:20:00' ) GROUP BY itemID ORDER BY sortval ASC LIMIT 0, 1
0.7905  0.0177  SELECT FOUND_ROWS() AS `count`
0.8074  0.0169  Using template: /templates/blog/post_page_next.html
0.8081  0.0007  SELECT * FROM perch2_categories WHERE catID=23 LIMIT 1
0.8279  0.0198  SELECT * FROM perch2_categories WHERE catID=24 LIMIT 1
0.844   0.0161  SELECT * FROM perch2_categories WHERE catID=25 LIMIT 1
0.8597  0.0156  SELECT * FROM perch2_categories WHERE catID=26 LIMIT 1
0.8758  0.0162  SELECT * FROM perch2_categories WHERE catID=27 LIMIT 1
0.8919  0.0161  SELECT * FROM perch2_categories WHERE catID=28 LIMIT 1
0.9089  0.017   SELECT * FROM perch2_categories WHERE catID=29 LIMIT 1
0.9255  0.0166  SELECT idx.itemID FROM perch2_blog_index idx JOIN perch2_blog_posts main ON idx.itemID=main.postID AND idx.itemKey='postID' AND (idx.indexKey='_category' AND idx.indexValue LIKE 'blog/team/%' OR idx.indexKey='_category' AND idx.indexValue='blog/team/') OR (idx.indexKey='_category' AND idx.indexValue LIKE 'blog/tickets/%' OR idx.indexKey='_category' AND idx.indexValue='blog/tickets/') OR (idx.indexKey='_category' AND idx.indexValue LIKE 'blog/community-education/%' OR idx.indexKey='_category' AND idx.indexValue='blog/community-education/') OR (idx.indexKey='_category' AND idx.indexValue LIKE 'blog/hospitality/%' OR idx.indexKey='_category' AND idx.indexValue='blog/hospitality/') OR (idx.indexKey='_category' AND idx.indexValue LIKE 'blog/business-club/%' OR idx.indexKey='_category' AND idx.indexValue='blog/business-club/') OR (idx.indexKey='_category' AND idx.indexValue LIKE 'blog/academy/%' OR idx.indexKey='_category' AND idx.indexValue='blog/academy/') OR (idx.indexKey='_category' AND idx.indexValue LIKE 'blog/featured/%' OR idx.indexKey='_category' AND idx.indexValue='blog/featured/')
0.9419  0.0164  SELECT * FROM perch2_blog_sections WHERE sectionSlug='blog' LIMIT 1
0.9568  0.0149  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' AND idx.itemID IN ('2', '2', '2', '2', '2', '2', '1', '1', '1', '1', '1', '1', '1', '3', '3', '3', '3', '3', '3', '2', '2', '2', '2', '2', '2', '2', '1', '1', '1', '1', '1', '1', '3', '3', '3', '3', '3', '3', '2', '2', '2', '2', '2', '2', '1', '1', '1', '1', '1', '1', '3', '3', '3', '3', '3', '3', '3') WHERE 1=1 AND ((idx.indexKey='postDateTime' AND idx.indexValue != '2014-12-09-warriors-get-ready-for-world-club-series')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID ) as tbl WHERE ( sectionID IS NULL AND postStatus='Published' AND postDateTime<='2015-03-30 17:20:00' ) GROUP BY itemID ORDER BY sortval ASC LIMIT 0, 3
0.9771  0.0203  SELECT FOUND_ROWS() AS `count`
0.9929  0.0158  Using template: /templates/blog/related_posts.html
Drew McLellan

Drew McLellan 2638 points
Perch Support

sectionID IS NULL is interesting. What are your blog sections called?

I have one section in my categories and it is called blog

Drew McLellan

Drew McLellan 2638 points
Perch Support

I believe you're thinking of category sets, which are different. Under Blog there is Sections. In your code, you've specified

'section' => 'blog'

is that an accurate blog section? If not, remove that option.

That worked, thanks for that.