4

I have the following code:

<ul>
  {exp:channel:entries channel="activity"}
    {related_entries id="activity_location"}
    <li><a href="{url_title_path='locations'}">{title}</a></li>
    {/related_entries}
  {/exp:channel:entries}
 </ul>

This outputs all activities (activity) that have an activity location (activity_location is the channel field with the relationship), so I get multiple results which is fine, but a lot of these multiple results are the same. I want to only output one of each.

For example currently I'll get:

England, Wales, England, England, Wales, Scotland

I want it to simply output:

England, Wales, Scotland

Is there any easy way of doing this?

3
  • First of all, why do you have multiple relationships called the same thing? Do you simply have multiple channel entries called the same thing? Oh hang on, scrub that, i see what you mean! Commented Feb 14, 2013 at 13:22
  • So to clarify: you want to generate a distinct list of all Activity Locations which have been selected by at least one entry in the Activities channel? Commented Feb 14, 2013 at 13:30
  • Yes that's correct
    – Nick
    Commented Feb 15, 2013 at 12:18

2 Answers 2

4

Unless this is a really simple site I would highly recommend switching to Playa relationships - you'll have lots more flexibility as the site grows. However, if you want to stick with native relationships, try using the unique parameter offered by Stash's get_list. Something like this works, whilst having the added benefit of keeping your markup cleanly separated from the data fetching part:

{exp:stash:set_list name="locations" parse_tags="yes"}
  {exp:channel:entries channel="activity"}
    {related_entries id="activity_location"}
        {stash:location}{title}{/stash:location} 
        {stash:url}{url_title_path='locations'}{/stash:url}                       
    {/related_entries}
  {/exp:channel:entries}
{/exp:stash:set_list}

<ul>
  {exp:stash:get_list unique="yes" name="locations"}
     <li><a href="{url}">{location}</a></li>
  {/exp:stash:get_list}
</ul>
1
  • Thanks James, will take a look at Stash and let you know how I get on!
    – Nick
    Commented Feb 15, 2013 at 12:20
0

Updated: The solution I first posted (below) was for the unique entries that match 1 parent entry. This query should pull all unique entries for your channel:

<ul>
  {exp:channel:entries channel="activity"}
    {exp:query sql="Select distinct t.title related_title, t.url_title related_url_title
                    from exp_relationships r join exp_channel_titles t
                    on r.rel_child_id = t.entry_id
                    where rel_parent_id in (Select entry_id from exp_channel_titles where channel_id = 1)"}
    <li><a href="/locations/{related_url_title}">{related_title}</a></li>
    {/exp:query}
  {/exp:channel:entries}
</ul>

Replace channel_id=1 with your channel's ID. Or use:

Select distinct t.title related_title, t.url_title related_url_title
                        from exp_relationships r join exp_channel_titles t
                        on r.rel_child_id = t.entry_id
                        where rel_parent_id in (Select entry_id from exp_channel_titles tt join exp_channel w
                        on tt.channel_id = w.channel_id where w.channel_name = 'activity')

Hopefully that works. The only site I have handy that uses native relationships is a 1.6.x site and the table/field names are different, so I'm writing the queries one way and then fixing them.

As was suggested in another answer, Playa offers many more options for this sort of thing, and conversion instructions.

First posting, that pulled related entries for 1 entry.

<ul>
  {exp:channel:entries channel="activity"}
    {exp:query sql="Select t.title related_title, t.url_title related_url_title
                    from exp_relationships r join exp_channel_titles t
                    on r.rel_child_id = t.entry_id
                    where rel_parent_id = {entry_id}"}
    <li><a href="/locations/{related_url_title}">{related_title}</a></li>
    {/exp:query}
  {/exp:channel:entries}
</ul>

The exp_relationships table is pretty straightforward.

3
  • Be aware, however, that if you are allowing user input (such as using a {segment_n} variable), the Query Module IS NOT SAFE and SHOULD NOT BE USED.
    – adrienne
    Commented Feb 14, 2013 at 17:07
  • The second query, which is the closest to the original post, would be the safest, because it's an explicitly typed channel name in the template. The 1st query is just as safe - It's a template-stored channel ID. Also be aware that the Query module offers better performance than other tags :) Commented Feb 14, 2013 at 18:06
  • Thanks will give this a go and let you know how I get on
    – Nick
    Commented Feb 15, 2013 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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