Forum
Filter blog posts by matching categories against another
Hi there,
I'm struggling with a particular issue which I was hoping someone could help me with. On my blog there is a category of articles called Press Coverage
On each blog post I am looking to output these articles, which is what i've done
perch_blog_custom(array(
'filter' => 'postSlug',
'match' => 'neq',
'value' => perch_get('s'),
'category' => REGION.'/coverage',
'count'=> 5,
'blog' => REGION,
'sort' => 'postDateTime',
'sort-order' => 'DESC',
'template'=> 'blog/post_coverage.html'
));
The part i'm struggling with is I want to filter these press coverage article to only show ones that are related to a different category.
So I have created a separate category set called Segments
with the following options
- Foodservice
- Building Care
- Industry
- Health Care
- Automotive
- Aerospace
On each blog post, the client has to select which segment or segments it belongs to.
So If they add a press article and selects Building Care
as the segment I want this press article to appear on every post that has a category of Building Care
.
But I am struggling to figure out how to do this?
I've done something like this but i'm not sure if im on the right path
$segment = perch_blog_custom(array(
'filter' => 'postSlug',
'match' => 'eq',
'value' => perch_get('s'),
'template'=> 'blog/segment.html'
), true);
perch_blog_custom(array(
'filter' => 'postSlug',
'match' => 'neq',
'value' => perch_get('s'),
'category' => REGION.'/coverage', "$string",),
'count'=> 5,
'blog' => REGION,
'sort' => 'postDateTime',
'sort-order' => 'DESC',
'template'=> 'blog/post_coverage.html'
));
// HTML TEMPLATE for segment.html is
<perch:categories id="segments" set="segments" label="Segments" display-as="checkboxes">
segments/<perch:category id="catSlug" type="slug" />
</perch:categories>
Can someone please help me with this
Thanks Barry
You'll need to use
skip-template
and thereturn-html
option so you can get both the data and the markup.echo the markup as usual.
Then fish out the related category from the data and requery for the posts in the related category.
Hi Drew,
Thanks for that, my php skills aren't the best but i've had a go with the following
I'm just not sure how to requery the posts in the related category.
The above code outputs the articles i'm just not sure how to link the two up and requery.
Do you have the category data?
I am now able to output the category selected and the coverage articles
The last bit is requering them, i'm not sure about that part.
Thanks for your help so far
Well this
echo($cats[0]['catSlug']);
only outputs the first category in the set called segmentsOk, i've got this working to a point
This outputs articles that has the same segment category, I just need to add in the coverage category thats in a separate set. Not sure how to do that.
I tried adding this
But its not having it, comes out like
I'm essentially trying to output different categories from different sets
Ok i've got this working to a point like so,
The problem i'm having now is on some articles, the segments category set hasn't been used, so when that happens we get an error saying:
Is there a way to remove the related press coverage section if the segment set hasn't been used?
Thanks
I wrapped it in an if statement
Seems to work ok, Is this what you were thinking from your first reply? Or have I done it completely different?
Thanks for the help
If it's working, then I think you're probably good.