This question already has an answer here:

this post gives me a good idea of the basic approach of filters:

Filter product collection by custom attribute

However, products have a custom attribute that is actually going to be stored as a multiselect element (which I assume is treated as an array):

$product->setAttribute('schoolid','11,12,13,18'); //the schoolid attribute for a given product
//or perhaps it's stored as an array
$product->setAttribute('schoolid', array(11,12,13,13));

this needs to be compared against a user's value so a this won't really work:

$collection->addFieldToFilter('schoolid', '13');

as this is more of an array comparison. Is there a standardized way to do this in Magento? Is there any way in other words to convert the product attribute to an array and do an in-array comparison within the existing model?

marked as duplicate by Fabian Schmengler, Raphael at Digital Pianism, Qaisar Satti, Moose, Anna Völkl Mar 10 '16 at 11:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • explain more whats u wants and why minus rate. – ravi patel Mar 9 '16 at 9:44

You can convert your school ids to array and then apply the filter.

$schoolid = '11,12,13,18'; //the schoolid attribute for a given product
$schoolIds = explode(',', $schoolid);

    //->addFieldToFilter('schoolid', '13');
    ->addFieldToFilter('schoolid', array('in' => $schoolIds));

This should do the trick for you.


If schoolid is a attribute type then filter collection like this:

    //->addFieldToFilter('schoolid', '13');
    ->addAttributeToFilter('schoolid', array('in' => $schoolIds));
  • 1
    This is interesting, why down vote? – Adarsh Khatri Mar 9 '16 at 9:39
addAttributeToFilter() is used to filter EAV collections.

addFieldToFilter() is used to filter Non-EAV collections.



n - in

$collection->addFieldToFilter('schoolid',array("in"=>array('11', '12', '13','18')))

Not In - nin

$collection->addFieldToFilter('schoolid',array("nin"=>array('11', '12', '13','18')))

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