Retired Documentation:  You are using the documentation for version 1.7.3 which was retired in 2013. Go here for the latest version documentation or check here for your available upgrades to the latest version.

Relating Entries to Other Entries
Creating Data Relationships for Increased Power

ExpressionEngine allows you to create relationships between entries in different weblogs, or between weblog entries and Gallery entries.

Why would you relate entries to other entries?

Here is an example to illustrate why someone would want to create relationships between entries.

The Night Club Owner

Imagine that you run a night club in which different musical artists perform nightly. To help promote your club you decide to create an ExpressionEngine weblog to display information about each night's events.

Each day you submit a new entry with information about a particular night's performances. You also include detailed information about each performer (names, bio, discography, etc.), but since many of the performers appear regularly at your club, over time you end up with a lot of duplicated information in your entries. And if a piece of information about a performer changes you have to updated it in every entry that mentions them. Before long you realize that this is not very efficient.

If only you could maintain a second weblog in which you store only information about each performer, and then connect it somehow to the main weblog so that you would never have to duplicate information. Guess what? You can with relationships.

In the above scenario, your primary weblog (let's call the weblog Events) would contain only information that is unique about a particular night, like the date, time, any special pricing, etc., and the second weblog (let's call the weblog Performers) would contain only information about each performer. You would then dynamically pull information from each performer into the appropriate Events weblog. To your site visitors the information appears just like a normal weblog entry, but internally, ExpressionEngine is pulling information from one entry and showing it within another.

The following graphic illustrates the concept. The "events" weblog on the left is showing information from the "performers" weblog on the right. Only a relationship exists between the entries. The data itself is never duplicated.

Each weblog entry can be related to one or more entries from other Weblogs and/or to entries from your Image Galleries.

Setting Up A Relationship

There are two steps to setting up a relationship, described in detail next:

  1. Create the relationship between a weblog and another (or a gallery).
  2. Add "related entry" tags to your Templates so the related data can be shown.

Once your relationship is set up, whenever you access the PUBLISH page to submit new content you'll see a drop-down menu showing the titles of all the entries in the related weblog or gallery. Choose a particular entry from the list and voila... the entries will be related.

1. Create the Relationship

A relationship is defined in the Custom Field Manager, located in your Control Panel here:

Admin > Weblog Management > Custom Weblog Fields

Custom fields can be one of several types, including Relationship types. When you set up a custom field as a relationship type you'll be asked to choose which weblog or gallery to associate it with. Once that association is set you'll see a drop-down menu in the PUBLISH page showing the titles of all the entries in the related weblog or gallery.

2. Add Related Entry Tags to your Template

To show a related entry, you will use the following tags:

{related_entries id="field_name"}

{/related_entries}

Note: The id parameter must contain the Field Name of the custom field that contains the relationship. For example, if your custom field is called performers you'll use this:

{related_entries id="performers"}

{/related_entries}

Important: The above tags must be placed inside your {exp:weblog:entries} tag.

Here is an example showing the related entries tags inside your weblog entry tags:

{exp:weblog:entries weblog="news" limit="15"}

<h3>{title}</h3>

{body}

{related_entries id="performers"}
 <h2>{title}</h2>
 {body}
{/related_entries}

Submitted on: {entry_date format='%M %d, %Y'}

{/exp:weblog:entries}

{if no_related_entries} Conditional

The {if no_related_entries} conditional allows you specify certain content to be displayed when an entry does not have a related entry for the field specified.

{exp:weblog:entries weblog="news" limit="15"}

<h3>{title}</h3>

{body}

{related_entries id="performers"}
 {if no_related_entries}
  <h2>No Performer Information Available</h2>
 {/if}
 <h2>{title}</h2>
 {body}
{/related_entries}

Submitted on: {entry_date format='%M %d, %Y'}

{/exp:weblog:entries}

What Type of Related Data can be Displayed?

Nearly anything that is available in the weblog entries and gallery entries tags can be shown as related data.

Important:  For performance reasons all related entry data is cached internally by the system when you submit new entries using the Publish page. This has the unfortunate side effect of making a some items that are normally dynamic not able to be used. These include various statistical variables:

{view_count_one}
{view_count_two}
{view_count_three}
{view_count_four}
{expiration_date }
{comment_expiration_date}
{recent_comment_date}
{comment_total}
{trackback_total}

Examples:

Gallery:  To show a Gallery Entry within a weblog entry you can do something similar to this:

{exp:weblog:entries weblog="news" limit="15"}

<h3>{title}</h3>

{body}

{related_entries id="gallery"}

   <div class="gallery">
   <img src="{image_url}" width="{width}" height="{height}" border="0" alt="{title}" title="{title}" />
   </div>
   <h3>{title}</h3>
   {caption}

{/related_entries}

Submitted on: {entry_date format='%M %d, %Y'}

{/exp:weblog:entries}

Note: Do not place the opening/closing gallery tags within the related tags. ONLY put the variables and HTML you'd like to appear.  Also note that you can only create relationships from a weblog entry to a gallery entry. You can not do it in reverse, that is, submit a new gallery entry with a pointer to a weblog entry.

Weblog:  To relate a weblog entry to another you'll do something similar to this:

{exp:weblog:entries weblog="news" limit="15"}

<h3>{title}</h3>

{body}

{related_entries id="weblog"}

   {date_heading}
   <h3 class="date">{entry_date format=' %l, %F %d, %Y '}</h3>
   {/date_heading}

   <h2 class="title">{title}</h2>
   {summary}

   <div class="posted">Posted by <a href="{profile_path='member/index'}">{author}</a> on {entry_date format='%m/%d'}

   {categories}
   <a href="{path='SITE_INDEX'}">{category_name}</a>
   {/categories}

{/related_entries}

Submitted on: {entry_date format='%M %d, %Y'}

{/exp:weblog:entries}

As indicated above: Do not place the opening/closing entry tags within the related tags. ONLY put the variables and HTML you'd like to appear.

Top of Page