<p>From your code example, it looks like you have several potential issues.  I'll outline them here and might need to revise this answer after you provide an update on how these things pan out.</p>

<p>It's unclear if you are trying to do this on an individual blog post page that has the <code>entry</code> variable already available to the template or if you are trying to do something more complex. If this is individual blog page as you mention in your comments the <code>entry</code> variable should already be available for you and you may need to remove the for loop (<code>{% for entry in craft.entries %}</code>) you have wrapped around your Matrix field code which is trying to loop through all your blog entries. On an individual entry page in a Section with URLs enabled, you can access your matrix field directly using <code>entry.matrixHandle</code>.</p>

<pre><code>{# You can probably remove this wrapping for loop #}
{% for entry in craft.entries %}

  {# You can start looping through your matrix here #}
  {% for block in entry.matrix %}

    {# This is good, you want to make sure you are within the
       block you are trying to access #}
    {% if block.type == "body" %}

      {# I've moved your SEARCH code below so we can focus on it a bit more easily. We'll put it back here afterwards. #}

    {% endif %}
  {% endfor %}

{# You can probably remove this wrapping endfor loop #}
{% endfor %}
</code></pre>

<p>It looks like you also may have the wrong syntax or approach for your search query (<code>.search('block.body:pizza')</code>).  You're right that you can dynamically pass content to the search parameter within your matrix loop but:</p>

<ol>
<li><p>You are trying to pass data from a Rich Text field which will not have a single word, but a whole bunch of words and not make for a very specific search.</p></li>
<li><p>Your syntax is incorrect.</p></li>
</ol>

<hr>

<p>Try simplifying your search to something hard coded that you know exists to get this to work.</p>

<pre><code>craft.entries.section('recipes').search('pizza')
</code></pre>

<p>If that works, they you need to sort out how to pass a more specific dynamic variable.  I don't think this is going to work from your <code>body</code> field, but it could work from a more targeted field.</p>

<pre><code>craft.entries.section('recipes').search(block.keywordField)
</code></pre>

<p>In this second example, I'm passing the variable directly, not wrapping it in quotation marks.  And I'm targeting a field called 'keywordField' that just has the word I want to search for not a big paragraph of text.</p>

<p>Alternatively, you may not be trying to search for a keyword available in your Matrix, and you may just be trying to find related content in your Recipes section to your blog post using a Entries Relations Field.  If this is the case, you may not need the search parameter at all, but relatedTo instead.</p>

<pre><code>craft.entries.section('recipes').relatedTo(entry)
</code></pre>

<p>In the end, you're loop may look something like this:</p>

<pre><code>{% for block in entry.matrix %}

  {% if block.type == "keywordField" %}

    {# Using a more targeted keyword field for the search #}
    {% for recipe in craft.entries.section('recipes').search(block.keywordField).order('score') %} 
        {{ recipe.title }}
    {% endfor %}

  {% endif %}

  {% if block.type == "body" %}

    {# Pulling in entries from an Entries Relations field #}
    {% for recipe in craft.entries.section('recipes').relatedTo(entry) %}
        {{ recipe.title }}
    {% endfor %}

  {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-08-19T07:13:33.817Z
2015-08-19T07:13:33.817Z
Ben Parizek
<p>I know there are some similar questions out there but none of the solutions suits my needs.</p>

<p>What I'm trying to do is to display some related entries (from another section) within my blog posts.</p>

<p>Here is what I have right now. Not working (obviously, it's me :))</p>

<pre><code>{% for entry in craft.entries %}
    {% for block in entry.matrix %}
        {% if block.type == "body" %}

                {% for recipe in craft.entries.section('recipes').search('block.body:pizza').order('score') %}
                    {{ recipe.title }}
                {% endfor %}

        {% endif %}
    {% endfor %}
{% endfor %}
</code></pre>

<p>Please advise.</p>

CC BY-SA 3.0
2015-06-05T09:19:43.840Z
Anyone please? *_*
Mark H.
<matrix><field><search>
Search within Matrix Field
# Search within Matrix Field

(Asked by: username_0 on 2015-05-24)

I know there are some similar questions out there but none of the solutions suits my needs.
What I'm trying to do is to display some related entries (from another section) within my blog posts.
Here is what I have right now. Not working (obviously, it's me :))
{% for entry in craft.entries %}
    {% for block in entry.matrix %}
        {% if block.type == "body" %}

                {% for recipe in craft.entries.section('recipes').search('block.body:pizza').order('score') %}
                    {{ recipe.title }}
                {% endfor %}

        {% endif %}
    {% endfor %}
{% endfor %}

Please advise.

**Tags:** matrix, field, search
> **Comments:**
> **username_0**: Anyone please? *_*  

---

## 1 answers

### Answer by username_1 on 2015-08-19. Score: 2

From your code example, it looks like you have several potential issues.  I'll outline them here and might need to revise this answer after you provide an update on how these things pan out.
It's unclear if you are trying to do this on an individual blog post page that has the entry variable already available to the template or if you are trying to do something more complex. If this is individual blog page as you mention in your comments the entry variable should already be available for you and you may need to remove the for loop ({% for entry in craft.entries %}) you have wrapped around your Matrix field code which is trying to loop through all your blog entries. On an individual entry page in a Section with URLs enabled, you can access your matrix field directly using entry.matrixHandle.
{# You can probably remove this wrapping for loop #}
{% for entry in craft.entries %}

  {# You can start looping through your matrix here #}
  {% for block in entry.matrix %}

    {# This is good, you want to make sure you are within the
       block you are trying to access #}
    {% if block.type == "body" %}

      {# I've moved your SEARCH code below so we can focus on it a bit more easily. We'll put it back here afterwards. #}

    {% endif %}
  {% endfor %}

{# You can probably remove this wrapping endfor loop #}
{% endfor %}

It looks like you also may have the wrong syntax or approach for your search query (.search('block.body:pizza')).  You're right that you can dynamically pass content to the search parameter within your matrix loop but:

You are trying to pass data from a Rich Text field which will not have a single word, but a whole bunch of words and not make for a very specific search.
Your syntax is incorrect.


Try simplifying your search to something hard coded that you know exists to get this to work.
craft.entries.section('recipes').search('pizza')

If that works, they you need to sort out how to pass a more specific dynamic variable.  I don't think this is going to work from your body field, but it could work from a more targeted field.
craft.entries.section('recipes').search(block.keywordField)

In this second example, I'm passing the variable directly, not wrapping it in quotation marks.  And I'm targeting a field called 'keywordField' that just has the word I want to search for not a big paragraph of text.
Alternatively, you may not be trying to search for a keyword available in your Matrix, and you may just be trying to find related content in your Recipes section to your blog post using a Entries Relations Field.  If this is the case, you may not need the search parameter at all, but relatedTo instead.
craft.entries.section('recipes').relatedTo(entry)

In the end, you're loop may look something like this:
{% for block in entry.matrix %}

  {% if block.type == "keywordField" %}

    {# Using a more targeted keyword field for the search #}
    {% for recipe in craft.entries.section('recipes').search(block.keywordField).order('score') %} 
        {{ recipe.title }}
    {% endfor %}

  {% endif %}

  {% if block.type == "body" %}

    {# Pulling in entries from an Entries Relations field #}
    {% for recipe in craft.entries.section('recipes').relatedTo(entry) %}
        {{ recipe.title }}
    {% endfor %}

  {% endif %}

{% endfor %}


---

