I am trying to get the entries in my custom plugin template ( CP )

Each entry has a lightswitch I want to fetch only those where lightswitch is enabled.

In controller file:

$criteria = craft()->elements->getCriteria(ElementType::Entry);
    'field' => 'lightswitchFieldHandle'
$criteria->limit = 20;
$products['entries'] = $criteria->find();
$this->renderTemplate('sponsoredentries/_index', $products);

But I am getting 20 entries (as per limit but not as per field)

1 Answer 1


You need to set the value of your Lightswitch field directly instead of using the relatedTo parameter

$criteria->lightswitchFieldHandle = '1';
  • ok will try as you suggest, but can you please help to understand that when we use relatedTo and when we use the way you suggested.Thank You.
    – inrsaurabh
    Commented Feb 27, 2019 at 16:14
  • the relatedTo parameter is only used when you want to filter for related elements. So when you have users, entries, categories or assets fields. For example when you want to display only entries with a certain category. You can learn more about that here docs.craftcms.com/v2/relations.html Commented Feb 27, 2019 at 16:39
  • Is this applied same when we fetch in fronted template.
    – inrsaurabh
    Commented Feb 28, 2019 at 8:40
  • There is no difference between Twig and PHP logic when it comes to Objects Commented Feb 28, 2019 at 8:57
  • You mean this way : gist.github.com/maddyBoy/db14ef067033b78837fc4e6fc351c1d2
    – inrsaurabh
    Commented Feb 28, 2019 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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