Forum

Thread tagged as: Problem, Blog

Listing out related blog posts with matching tags

Hi guys, I've been stuck for a while on this, all the documentation I've found refers to categories rather than tags, and only one category. What I want to do is list out all the blog posts matching the current posts tags, this is on my main post.php page.

So far I have:

$tags =

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

perch_blog_custom(array( 
'tag' => array($tags),
'count' => 10, 
'page-link-style' => 'all', 
'page-links' => 'true', 
'template' => 'post_in_list.html', 
'sort' => 'postDateTime', 
'sort-order' => 'RAND', 
'each' => function($item) { $item['tags'] = perch_blog_post_tags($item['postSlug'], 'post_tag_link.html', true); return $item; }, 
)); 

It works as expected with:

'tag' => array('tag1','tag2','tag3'),

I just need to replace those with the tags taken from the current post.

Any help would be appreciated :)

Thanks!

David Clarke

David Clarke 0 points

  • 3 years ago
Duncan Revell

Duncan Revell 78 points
Registered Developer

I think your $tags variable is already an array, so try:

'tag' => $tags,
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello David,

If you inspect the value of $tags you will find it contains arrays of tags. Each array should have the tag ID, title and slug. So you probably can do something like this:

$tag_slugs = array();

foreach($tags as $tag) {
    $tag_slugs[] = $tag['tagSlug'];
}

perch_blog_custom(array( 
'tag' => $tag_slugs,
'count' => 10, 
'page-link-style' => 'all', 
'page-links' => 'true', 
'template' => 'post_in_list.html', 
'sort' => 'postDateTime', 
'sort-order' => 'RAND', 
'each' => function($item) { $item['tags'] = perch_blog_post_tags($item['postSlug'], 'post_tag_link.html', true); return $item; }, 
)); 

Cheers Hussein. I did print out the array before, but was unsure of how to reduce it down to just the titles, and if that was even the correct way to go about it in Perch. But this works great, thanks again :)

What is this function in php called? creating an array from individual parts of another array. Just so I can read up on it for future reference.

For anyone else looking for a similar solution this is my full code to display related posts which match the current posts tags. I've also filtered by the current post slug to remove the current post from the listing.

<?php

$tags =
perch_blog_post_tags(perch_get('s'), array(
'skip-template' => 'true',
'raw'=>'true',
));

$tag_slugs = array();  
foreach($tags as $tag) { 
$tag_slugs[] = $tag['tagSlug']; 
}  

perch_blog_custom(array(
'filter' => 'postSlug', 
'match' => 'neq', 
'value' => perch_get('s'),
'tag' => $tag_slugs, 
'count' => 10,  
'page-link-style' => 'all',  
'page-links' => 'true',  
'template' => 'post_in_list.html',  
'sort' => 'postDateTime',  
'sort-order' => 'RAND',  
'each' => function($item) { $item['tags'] = perch_blog_post_tags($item['postSlug'], 'post_tag_link.html', true); return $item; },  )); 

?>
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

What is this function in php called? creating an array from individual parts of another array. Just so I can read up on it for future reference.

There isn't much going on there. We're populating the array $tag_slugs with values from the array $tags. To get the values from $tags, we loop through its elements using a foreach loop

So I guess you can read up on arrays and foreach if you're not already familiar with them.