I am using a content management plugin that lets you create posts and assign them related categories.

My issue is that if I create a post that is relevant to two categories then the realted categories are stored in an array style e.g.

Categories: 3,5

If I then am on a product and want to pull in posts related to this category I use an ->addAttributeToFilter(); on my collection as below:

$catid = 5
->addAttributeToFilter('linked_categories', $catid);

But obviously the "linked_categories" array is 3,5 so my variable doesn't match!

How can I say "If the linked_categories array contains my $catid"?


Mysql has the FIND_IN_SET function for that. With Magento collections you can use it as follows:

addAttributeToFilter('linked_categories',  ['finset' => 5]);

Like so

addAttributeToFilter('linked_categories', array('in' => $catid));
  • no worries enjoy – Bobadevv Feb 1 '16 at 16:15
  • 1
    @Bobadevv this works if $catId is an array and the value in the attribute is a single id. But from KiwisTasteGood's description the value in the attribute is comprised of ids separated by comma. In this case he should do a regex on the attribute to correctly filter the collection – Alex Dinca Feb 1 '16 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.