<p>You need to set the value of your Lightswitch field directly instead of using the <code>relatedTo</code> parameter</p>

<pre class="lang-PHP prettyprint-override"><code>$criteria-&gt;lightswitchFieldHandle = '1';
</code></pre>

CC BY-SA 4.0
2019-02-27T16:14:18.050Z
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
CC BY-SA 4.0
2019-02-27T16:39:10.487Z
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 https://docs.craftcms.com/v2/relations.html
Robin Schambach
CC BY-SA 4.0
2019-02-28T08:40:08.530Z
Is this applied same when we fetch in fronted template.
inrsaurabh
CC BY-SA 4.0
2019-02-28T08:57:51.733Z
There is no difference between Twig and PHP logic when it comes to Objects
Robin Schambach
CC BY-SA 4.0
2019-02-28T09:32:16.283Z
You mean this way : 

https://gist.github.com/maddyBoy/db14ef067033b78837fc4e6fc351c1d2
inrsaurabh
CC BY-SA 4.0
2019-02-28T09:55:47.513Z
Ah you mean "how is it applied" not "if it is applied" yeah that should be correct
Robin Schambach
CC BY-SA 4.0
2019-02-28T09:58:10.800Z
Its working fine, i got only those entries where lightswitchFieldHandle = 1,
`Thank you`
inrsaurabh
CC BY-SA 4.0
2019-02-28T09:59:31.543Z
Can you please see this issue of mine. 
https://craftcms.stackexchange.com/questions/29673/update-a-custom-field-lightswitchfieldhandle-in-custom-plugin-controller-folde
inrsaurabh
CC BY-SA 4.0
2019-02-27T16:11:00.007Z
2019-02-27T16:11:00.007Z
Robin Schambach
<p>I am trying to get the entries in my custom plugin template ( CP )</p>

<p>Each entry has a lightswitch I want to fetch only those where lightswitch is enabled.</p>

<p>In controller file:</p>

<pre class="lang-PHP prettyprint-override"><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section('articles');
$criteria-&gt;relatedTo(array(
    'field' =&gt; 'lightswitchFieldHandle'
));
$criteria-&gt;limit = 20;
$products['entries'] = $criteria-&gt;find();
$this-&gt;renderTemplate('sponsoredentries/_index', $products);
</code></pre>

<p>But I am getting 20 entries (as per limit but not as per field)</p>

<plugin-development><craft2>
Get entries based on custom field lightswitch in custom plugin in Craft 2
# Get entries based on custom field lightswitch in custom plugin in Craft 2

(Asked by: username_0 on 2019-02-27)

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);
$criteria->section('articles');
$criteria->relatedTo(array(
    '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)

**Tags:** plugin-development, craft2

---

## 1 answers

### Answer by username_1 on 2019-02-27. Score: 1 | Accepted answer

You need to set the value of your Lightswitch field directly instead of using the relatedTo parameter
$criteria->lightswitchFieldHandle = '1';

> **Comments:**
> **username_0**: 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.  
> **username_1**: 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 https://docs.craftcms.com/v2/relations.html  
> **username_0**: Is this applied same when we fetch in fronted template.  
> **username_1**: There is no difference between Twig and PHP logic when it comes to Objects  
> **username_0**: You mean this way :   https://gist.github.com/maddyBoy/db14ef067033b78837fc4e6fc351c1d2  
> **username_1**: Ah you mean "how is it applied" not "if it is applied" yeah that should be correct  
> **username_0**: Its working fine, i got only those entries where lightswitchFieldHandle = 1, `Thank you`  
> **username_0**: Can you please see this issue of mine.  https://craftcms.stackexchange.com/questions/29673/update-a-custom-field-lightswitchfieldhandle-in-custom-plugin-controller-folde  

---

