Channel Entries within Channel Entries : The Solutions : ExpressionEngine

Most ExpressionEngine work is done with the powerful and flexible channel entries tag. It's the one where you pull the data / content from your entries as you know them within the control panel. It's by far the most used tag and you need to know it well.

There comes situations where you want a channel entries tag pair to run within a channel entries tag pair. Thinking about it for the first time and you would think "ah thats easy, I just type it out and if by magic I can run it..."

All the following examples are searching a body like custom field named cf_meat_body and using it with the no_results tag pair. Even if you are new to EE i'm sure you can guess what the no_results syntax means.. Yes.. no results. You would think the below work wouldn't you?

{exp:channel:entries channel=“meat“ limit=“10“  search:cf_meat_body=“steak“ }
    
    {title}
    .....OTHER Custom Fields
    
    {if no_results}
        <h1>There are no results but please see these entries</h1>
        {exp:channel:entries channel=“meat“ limit=“10“ }
            {title}
            ....OTHER Custom Fields
        {/exp:channel:entries}
    {/if}
    
{/exp:channel:entries}

Unfortunately not, by default you cannot run a channel entries tag pair within a channel entries tag pair in a single template. That doesn't mean you cannot do it, you just cannot doing without either using an embed, using Low Variables

Solution 1: Traditional Embed

Lets look at the first example which will work but having to using an embed.


{exp:channel:entries channel=“meat“ limit=“10“ search:cf_meat_body=“steak“}
  
     {if no_results}
          <h1>There are no results but please see these entries</h1>
          {embed=“templategroup/embedname“}
     {/if}
   
   {title}
    .....OTHER Custom Fields
{/exp:channel:entries}

In the embed:

{exp:channel:entries channel=“meat“ limit=“5“}
       {title}
    .....OTHER Custom Fields 
{/exp:channel:entries}

Solution 2: Low Variables - Third Party Add-on

A better alternative is to use Low Variables which increases the performance over embeds.

{exp:channel:entries channel=“tester“ limit=“1“ search:cf_meat_body=“steak“}
  
  {if no_results}
        <h1>There are no results but please see these entries</h1>
        {exp:low_variables:parse var=“lv_embed“}
   {/if}
   
     {title}
  .....OTHER Custom Fields
   
{/exp:channel:entries}

In the Low variable - saved as a file, you would have the same code as the embed example above.


{exp:channel:entries channel=“meat“ limit=“5“}
       {title}
    .....OTHER Custom Fields 
{/exp:channel:entries}

Notes / Caveats

These are superficial examples for the benefit of demonstration. I have tested these working in a single template however. You may run into problems with parse order or by the favourite ExpressionEngine duh moment dynamic = no. You would also add in disable=“categories|member_data|pagination|category_fields“ in a production environment.