<p>Perhaps just grab the URL segment and set it as a variable:</p>

<pre><code>{% set mID = craft.request.getSegment(2) %}
{% for entry in craft.entries.section('whatever') %}
   {% for block in entry.myMatrixField.id(mID) %}
     {{ block.someNestedFieldInsideOfMyMatrixField }}
   {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-07-09T13:49:41.513Z
Yeah, I was hoping to avoid looping through every entry...
Low
CC BY-SA 3.0
2014-07-09T13:51:18.827Z
Actually, if I remember correctly one of the discussions I had with Brandon, this wouldn't be too taxing in terms of queries. Craft is smart enough to only fetch what it needs (the entry_id in this case)
Jérôme Coupé
CC BY-SA 3.0
2014-07-09T15:16:37.523Z
Even with Yii's ORM, it still grabs the entries before it can grab the Matrix block. Ideally, yes, it would be great if there's an interface somehow directly into Matrix ID's. It wouldn't be a tough plugin to write though if there's no native way to do it.
jpea
CC BY-SA 3.0
2014-07-09T13:48:32.443Z
2014-07-09T13:48:32.443Z
jpea
<p>At the time of this writing, the ideal solution isn't possible. But, as <a href="https://craftcms.stackexchange.com/questions/856/can-i-get-an-entry-by-matrix-block-id/866#comment1366_857">jpea suggested</a>, creating a plugin was dead easy, so here it is:</p>

<p><a href="https://github.com/low/lowblocks" rel="nofollow noreferrer"><strong>Low Blocks</strong></a></p>

<p>...which lets me use syntax like this:</p>

<pre><code>{% set block = craft.lowBlocks.id(craft.request.segment(2)) %}
{% set entry = block.owner %}
</code></pre>

CC BY-SA 3.0
2014-07-09T18:00:00.490Z
Now that's the way to answer your own question, 4 hours later with the plugin you just wrote to solve it. ;)
Simon Kuran
CC BY-SA 3.0
2014-07-09T18:53:24.480Z
Heck yeah - nice work!
jpea
CC BY-SA 3.0
2014-07-09T21:06:50.703Z
Someday I'm going to answer my own question with my own plugin. Nicely done.
Matt Stein
CC BY-SA 3.0
2014-07-09T17:44:26.193Z
2014-07-09T17:44:26.193Z
2017-04-13T12:51:00.200Z
Low
<p>For a url like <code>domain.com/example/123</code> where <code>123</code> is a Matrix Block ID (not an Entry ID), can I get the associated entry for given block ID in a template?</p>

<p>Or, for a more general approach, what would be the best way to get that Matrix Block element and its Entry element when you only have a block ID?</p>

<p><strong>Edit:</strong> so ideally:</p>

<pre><code>{% set block = craft.matrixBlocks.id(craft.request.segment(2)).first() %}
{% set entry = block.getOwner() %}
</code></pre>

<p>...which isn't possible now.</p>

CC BY-SA 3.0
2014-07-09T13:45:04.907Z
Is 123 the block ID or the block index?
jpea
CC BY-SA 3.0
2014-07-09T13:46:31.250Z
The ID. _Not_ the index.
Low
CC BY-SA 3.0
2014-07-09T13:49:04.690Z
Gotcha - I posted something that might work below. I haven't tested it, but it's the gist of something that should work.
jpea
<entries><matrix>
Can I get an entry by Matrix Block ID?
# Can I get an entry by Matrix Block ID?

(Asked by: username_0 on 2014-07-09)

For a url like domain.com/example/123 where 123 is a Matrix Block ID (not an Entry ID), can I get the associated entry for given block ID in a template?
Or, for a more general approach, what would be the best way to get that Matrix Block element and its Entry element when you only have a block ID?
Edit: so ideally:
{% set block = craft.matrixBlocks.id(craft.request.segment(2)).first() %}
{% set entry = block.getOwner() %}

...which isn't possible now.

**Tags:** entries, matrix

> **Comments:**
> **username_1**: Is 123 the block ID or the block index?  
> **username_0**: The ID. _Not_ the index.  
> **username_1**: Gotcha - I posted something that might work below. I haven't tested it, but it's the gist of something that should work.  

---

## 2 answers

### Answer by username_0 on 2014-07-09. Score: 11 | Accepted answer

At the time of this writing, the ideal solution isn't possible. But, as username_1 suggested, creating a plugin was dead easy, so here it is:
username_0 Blocks
...which lets me use syntax like this:
{% set block = craft.lowBlocks.id(craft.request.segment(2)) %}
{% set entry = block.owner %}

> **Comments:**
> **username_3**: Now that's the way to answer your own question, 4 hours later with the plugin you just wrote to solve it. ;)  
> **username_1**: Heck yeah - nice work!  
> **username_4**: Someday I'm going to answer my own question with my own plugin. Nicely done.  

---

### Answer by username_1 on 2014-07-09. Score: 4

Perhaps just grab the URL segment and set it as a variable:
{% set mID = craft.request.getSegment(2) %}
{% for entry in craft.entries.section('whatever') %}
   {% for block in entry.myMatrixField.id(mID) %}
     {{ block.someNestedFieldInsideOfMyMatrixField }}
   {% endfor %}
{% endfor %}

> **Comments:**
> **username_0**: Yeah, I was hoping to avoid looping through every entry...  
> **username_2**: Actually, if I remember correctly one of the discussions I had with Brandon, this wouldn't be too taxing in terms of queries. Craft is smart enough to only fetch what it needs (the entry_id in this case)  
> **username_1**: Even with Yii's ORM, it still grabs the entries before it can grab the Matrix block. Ideally, yes, it would be great if there's an interface somehow directly into Matrix ID's. It wouldn't be a tough plugin to write though if there's no native way to do it.  

---

