1

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.

  • Anyone please? _ – Mark H. Jun 5 '15 at 9:19
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:

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

  2. 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 %}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.