Answers.Body: <p>I realize this question was geared towards getting the entry via twig. Here is how you could accomplish the same from a plugin. In my case, I wanted to find an entry via slug.</p>
  
  <p><strong>MyPluginController.php</strong></p>
  
  <pre><code>public function actionMyAction() {
      $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
      $criteria-&gt;section = 'mySectionHandle';
      $criteria-&gt;slug = $this-&gt;_getLastSegment(craft()-&gt;request-&gt;getUrlReferrer());
      ...
  
      $entries = $criteria-&gt;find();
  
      if ($entries) {
          foreach ($criteria as $entry) {
              error_log($entry-&gt;title);
          }
      } else {
          // No entry found that matches criteria.
      }
  }
  
  private function _getLastSegment($url)
  {
      $path = parse_url($url, PHP_URL_PATH);
      $trimmed = trim($path, '/');
      $tokens = explode('/', $trimmed);
  
      return end($tokens);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-11T20:31:24.360Z
Answers.LastActivityDate: 2016-03-13T19:00:22.127Z
Answers.LastEditDate: 2016-03-13T19:00:22.127Z
Answers.UserDisplayName: Damon
Answers.Body: <p>An alternate way would be to use Twig's <a href="https://twig.symfony.com/doc/2.x/filters/filter.html" rel="nofollow noreferrer"><code>|filter()</code></a> to do the filtering rather than Craft itself.</p>
  
  <p>I can't say how much of a performance difference it makes, but I know that if you're doing very large queries many times, without adequate caching, it could slow your site down if you're constantly making multiple entry queries.</p>
  
  <p>It also allows a bit more flexibility if you're trying to do something non-standard or need a function callback. For example, I wanted to create an array of IDs in Twig and then choose the first entry that matched it, and <a href="https://twig.symfony.com/doc/2.x/filters/filter.html" rel="nofollow noreferrer"><code>|filter()</code></a> worked well in that situation.</p>
  
  <pre><code>{% set featured = [18, 56, 105] %}
  {% set entries = craft.entries.section('myChosenSection').all() %}
  {% set chosenEntry = entries|filter((entry) =&gt; entry.id in featured)|first %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-03T12:17:29.310Z
Answers.LastActivityDate: 2019-08-03T12:22:30.427Z
Answers.LastEditDate: 2019-08-03T12:22:30.427Z
Answers.UserDisplayName: Matt Fletcher
Answers.Body: <p>This is the correct way.</p>
  <p><strong>Examples:</strong></p>
  <p><em>Singles:</em></p>
  <pre class="lang-twig prettyprint-override"><code>{% set entry = craft.entries.section('section_handle').one() %}
  </code></pre>
  <p><em>Channels / Structures:</em></p>
  <pre class="lang-twig prettyprint-override"><code>{% set entry = craft.entries.section('section_handle').slug('my_slug').one() %}
  </code></pre>
  <p><em>General:</em></p>
  <pre class="lang-twig prettyprint-override"><code>{% set entry = craft.entries.id(5).one() %}
  </code></pre>
  <p>See <a href="https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#parameters" rel="noreferrer"><code>craft.entries</code></a> for a list of all properties.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T13:12:22.170Z
Answers.Text: I would use {% set entry = craft.entries.section('myChosenSection').first() %} to get the first entry in a Channel or Sructure, and {% set entry = craft.entries.section('myChosenSection').find() %} to get the Single.
Answers.UserDisplayName: Andrea DeMers
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T14:49:26.450Z
Answers.Text: Thanks Victor, it too seemed to me counter intuitive to have to use `.first()` in a single, however it seems if I remove it then the request returns nothing, as does using `.find()` - have I got something wrong in setup?
Answers.UserDisplayName: pumkincreative
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T14:54:54.647Z
Answers.Text: Actually `first()` would still be necessary here. If you want to get an array of multiple entries back, it’s `find()` that’s not necessary, since `find()` is the default behavior.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T15:21:46.740Z
Answers.Text: So for Single:{% set entry = craft.entries.section('myChosenSection').first() %}, and for Channel/Structure array: {% set entry = craft.entries.section('myChosenSection').find() %} or {% set entry = craft.entries.section('myChosenSection') %}, and first item in Channel/Structure array: {% set entry = craft.entries.section('myChosenSection').first() %}?
Answers.UserDisplayName: Andrea DeMers
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T17:01:05.830Z
Answers.Text: @BrandonKelly Is there a reason `.first()` is necessary? Since there is only one entry which matches the criteria.
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T19:21:25.947Z
Answers.Text: @VictorIn `craft.entries` doesn’t care whether it’s a Single or a Channel or a Structure. It’s only responsible for taking some parameters, passing them off to the ElementsService, and then either returning an array of all the results (`find()`) or just the first result (`first()`). You can technically use `find()` with a Single section if you want; you’ll just get an array back of that one entry.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-18T08:26:35.680Z
Answers.Text: I believe using `.slug()` also requires `.first()` otherwise I can't seem to access any properties. ie `.slug('my_slug').first();`
Answers.UserDisplayName: Alexander Holsgrove
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2014-07-08T12:54:16.593Z
Answers.LastActivityDate: 2020-07-15T22:14:24.797Z
Answers.LastEditDate: 2020-07-15T22:14:24.797Z
Answers.UserDisplayName: Victor
Body: <p>Hopefully this is a simple question but I couldn't find a clear answer anywhere - I just want to be able to pull in data from a specific entry, where I know the identifier.</p>
  
  <p>In this particular case, the entry is a single. Currently I'm doing this:</p>
  
  
  
  <pre><code>{% set entry = craft.entries.section('myChosenSection').first() %}
  </code></pre>
  
  <p>Is this correct, or is there a more concise way?</p>
  
Tags: <entries><sections>
Title: How to return a specific entry?
ThreadText: # How to return a specific entry?
  
  (Asked by: username_0 on 2014-07-08)
  
  Hopefully this is a simple question but I couldn't find a clear answer anywhere - I just want to be able to pull in data from a specific entry, where I know the identifier.
  
  In this particular case, the entry is a single. Currently I'm doing this:
  
  ```
  {% set entry = craft.entries.section('myChosenSection').first() %}
  
  ```
  
  Is this correct, or is there a more concise way?
  
  **Tags:** entries, sections
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2014-07-08. Score: 39 | Accepted answer
  
  This is the correct way.
  
  **Examples:**
  
  *Singles:*
  
  ```twig
  {% set entry = craft.entries.section('section_handle').one() %}
  
  ```
  
  *Channels / Structures:*
  
  ```twig
  {% set entry = craft.entries.section('section_handle').slug('my_slug').one() %}
  
  ```
  
  *General:*
  
  ```twig
  {% set entry = craft.entries.id(5).one() %}
  
  ```
  
  See [`craft.entries`](https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#parameters) for a list of all properties.
  
  > **Comments:**
  > **username_4**: I would use {% set entry = craft.entries.section('myChosenSection').first() %} to get the first entry in a Channel or Sructure, and {% set entry = craft.entries.section('myChosenSection').find() %} to get the Single.  
  > **username_0**: Thanks username_3, it too seemed to me counter intuitive to have to use `.first()` in a single, however it seems if I remove it then the request returns nothing, as does using `.find()` - have I got something wrong in setup?  
  > **username_5**: Actually `first()` would still be necessary here. If you want to get an array of multiple entries back, it’s `find()` that’s not necessary, since `find()` is the default behavior.  
  > **username_4**: So for Single:{% set entry = craft.entries.section('myChosenSection').first() %}, and for Channel/Structure array: {% set entry = craft.entries.section('myChosenSection').find() %} or {% set entry = craft.entries.section('myChosenSection') %}, and first item in Channel/Structure array: {% set entry = craft.entries.section('myChosenSection').first() %}?  
  > **username_3**: @username_5 Is there a reason `.first()` is necessary? Since there is only one entry which matches the criteria.  
  > **username_5**: @VictorIn `craft.entries` doesn’t care whether it’s a Single or a Channel or a Structure. It’s only responsible for taking some parameters, passing them off to the ElementsService, and then either returning an array of all the results (`find()`) or just the first result (`first()`). You can technically use `find()` with a Single section if you want; you’ll just get an array back of that one entry.  
  > **username_6**: I believe using `.slug()` also requires `.first()` otherwise I can't seem to access any properties. ie `.slug('my_slug').first();`  
  
  ---
  
  ### Answer by username_1 on 2016-02-11. Score: 4
  
  I realize this question was geared towards getting the entry via twig. Here is how you could accomplish the same from a plugin. In my case, I wanted to find an entry via slug.
  
  **MyPluginController.php**
  
  ```
  public function actionMyAction() {
      $criteria = craft()->elements->getCriteria(ElementType::Entry);
      $criteria->section = 'mySectionHandle';
      $criteria->slug = $this->_getLastSegment(craft()->request->getUrlReferrer());
      ...
  
      $entries = $criteria->find();
  
      if ($entries) {
          foreach ($criteria as $entry) {
              error_log($entry->title);
          }
      } else {
          // No entry found that matches criteria.
      }
  }
  
  private function _getLastSegment($url)
  {
      $path = parse_url($url, PHP_URL_PATH);
      $trimmed = trim($path, '/');
      $tokens = explode('/', $trimmed);
  
      return end($tokens);
  }
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2019-08-03. Score: 1
  
  An alternate way would be to use Twig's [`|filter()`](https://twig.symfony.com/doc/2.x/filters/filter.html) to do the filtering rather than Craft itself.
  
  I can't say how much of a performance difference it makes, but I know that if you're doing very large queries many times, without adequate caching, it could slow your site down if you're constantly making multiple entry queries.
  
  It also allows a bit more flexibility if you're trying to do something non-standard or need a function callback. For example, I wanted to create an array of IDs in Twig and then choose the first entry that matched it, and [`|filter()`](https://twig.symfony.com/doc/2.x/filters/filter.html) worked well in that situation.
  
  ```
  {% set featured = [18, 56, 105] %}
  {% set entries = craft.entries.section('myChosenSection').all() %}
  {% set chosenEntry = entries|filter((entry) => entry.id in featured)|first %}
  
  ```
  
  
  ---
  
  