Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-27T16:14:18.050Z
Answers.Text: 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.
Answers.UserDisplayName: inrsaurabh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-27T16:39:10.487Z
Answers.Text: 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
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T08:40:08.530Z
Answers.Text: Is this applied same when we fetch in fronted template.
Answers.UserDisplayName: inrsaurabh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T08:57:51.733Z
Answers.Text: There is no difference between Twig and PHP logic when it comes to Objects
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T09:32:16.283Z
Answers.Text: You mean this way : 
  
  https://gist.github.com/maddyBoy/db14ef067033b78837fc4e6fc351c1d2
Answers.UserDisplayName: inrsaurabh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T09:55:47.513Z
Answers.Text: Ah you mean "how is it applied" not "if it is applied" yeah that should be correct
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T09:58:10.800Z
Answers.Text: Its working fine, i got only those entries where lightswitchFieldHandle = 1,
  `Thank you`
Answers.UserDisplayName: inrsaurabh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T09:59:31.543Z
Answers.Text: Can you please see this issue of mine. 
  https://craftcms.stackexchange.com/questions/29673/update-a-custom-field-lightswitchfieldhandle-in-custom-plugin-controller-folde
Answers.UserDisplayName: inrsaurabh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-27T16:11:00.007Z
Answers.LastActivityDate: 2019-02-27T16:11:00.007Z
Answers.UserDisplayName: Robin Schambach
Body: <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>
  
Tags: <plugin-development><craft2>
Title: Get entries based on custom field lightswitch in custom plugin in Craft 2
ThreadText: # 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:
  
  ```PHP
  $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
  
  ```PHP
  $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  
  
  ---
  
  