Forum

Thread tagged as: Error, Blog

Show list of related blog posts by category

I found a snippet of code online that showed an example of listing related blog posts (related by category) in random order, limit of 3, but not including the current post being viewed.

I tried the code and nothing....

If I comment out the IF statement I see related posts, but not sure how they are being sorted or if filtering is 100% correct as sort order seems to throw error. (See Below).

Code in page:


<?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['categorySlug']; } perch_blog_custom(array( 'filter' => 'postSlug', 'match' => 'neq', 'value' => perch_get('s'), 'category' => $cat_slugs, 'sort-order'=> RAND, 'count'=> 3, 'template'=> 'blog/related-cat.html', 'section'=> 'projects' )); } ?>

I turned on error checking and the sort order is undefined...

So not sure that we need IF statement anyway as it seems to work without it, but we need to fix sort order to be random.

Error:


Array ( [type] => 8 [message] => Use of undefined constant RAND - assumed 'RAND' [file] => /xxx/public_html/xxx/project-detail.php [line] => 34 )

Confirming we are running the latest Blog and Perch version. PHP is getting a bit old but that should be fine yeah?

Perch: 2.7.3, PHP: 5.3.29, MySQL: 5.5.37, with MySQLi Server OS: Linux, cgi-fcgi Installed apps: content (2.7.3), assets (2.7.3), categories (2.7.3), perch_blog (4.5.4), perch_forms (1.7) App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_blog', 'perch_forms', ); ?>

Thanks for any help!

Stuart Farrell

Stuart Farrell 0 points

  • 7 years ago

Just confirming, when commenting out IF statement, it shows 3 posts but these are NOT related posts (as in only ones in with at least one category the same as current post).

Drew McLellan

Drew McLellan 2638 points
Perch Support

RAND is a string so should be quoted 'RAND'.

What does this output?

perch_blog_post_categories(perch_get('s'));

That outputs the categories of the post as expected.

Drew McLellan

Drew McLellan 2638 points
Perch Support

And this?

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

Array ( [0] => Array ( [catID] => 5 [setID] => 1 [catParentID] => 0 [catTitle] => Diaphragm Walls [catSlug] => diaphragm-walls [catPath] => blog/diaphragm-walls/ [catDisplayPath] => Diaphragm Walls [catOrder] => 5 [catTreePosition] => 001-005 [catDynamicFields] => [] [catDepth] => 1 ) [1] => Array ( [catID] => 8 [setID] => 1 [catParentID] => 0 [catTitle] => Jet Grouting [catSlug] => jet-grouting [catPath] => blog/jet-grouting/ [catDisplayPath] => Jet Grouting [catOrder] => 8 [catTreePosition] => 001-008 [catDynamicFields] => [] [catDepth] => 1 ) )
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, change this:

$cat_slugs[] = $cat['categorySlug'];

to this:

$cat_slugs[] = $cat['catSlug'];

Awesome thanks! Fastest fix ever... :-)