<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>

CC BY-SA 3.0
2016-02-11T20:31:24.360Z
2016-03-13T19:00:22.127Z
2016-03-13T19:00:22.127Z
Damon
<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>

CC BY-SA 4.0
2019-08-03T12:17:29.310Z
2019-08-03T12:22:30.427Z
2019-08-03T12:22:30.427Z
Matt Fletcher
<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>

CC BY-SA 3.0
2014-07-08T13:12:22.170Z
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.
Andrea DeMers
CC BY-SA 3.0
2014-07-08T14:49:26.450Z
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?
pumkincreative
CC BY-SA 3.0
2014-07-08T14:54:54.647Z
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.
Brandon Kelly
CC BY-SA 3.0
2014-07-08T15:21:46.740Z
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() %}?
Andrea DeMers
CC BY-SA 3.0
2014-07-08T17:01:05.830Z
@BrandonKelly Is there a reason `.first()` is necessary? Since there is only one entry which matches the criteria.
Victor
CC BY-SA 3.0
2014-07-08T19:21:25.947Z
@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.
Brandon Kelly
CC BY-SA 3.0
2017-05-18T08:26:35.680Z
I believe using `.slug()` also requires `.first()` otherwise I can't seem to access any properties. ie `.slug('my_slug').first();`
Alexander Holsgrove
CC BY-SA 4.0
2014-07-08T12:54:16.593Z
2020-07-15T22:14:24.797Z
2020-07-15T22:14:24.797Z
Victor
<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>

<entries><sections>
How to return a specific entry?
# 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:
{% set entry = craft.entries.section('section_handle').one() %}

Channels / Structures:
{% set entry = craft.entries.section('section_handle').slug('my_slug').one() %}

General:
{% set entry = craft.entries.id(5).one() %}

See craft.entries 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 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?  
> **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**: @BrandonKelly 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() 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() 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 %}


---

