« Homepage

ExpressionEngine Related Entries Module

29/06/09
Glen Swinfield

ExpressionEngine related entries can be prohibitive. This module allows you to use full weblog:entries tag functionality for related entries. No hack required. (PHP 5 only)

You may already be using the "Better Related Entries" extension previously available from my personal site. We have been using it internally here at Erskine for a while and overall it seems that the EE community have been using it in a number of interesting ways. The fact that it required a hack to the core to get it to work has always been an irritation to me. No more. This re-loaded module version requires no hack. Here's how it works.

Tech Stuff

The module works by extending the EE weblog class, it runs a query to get related entries and inserts a pipe-separated list of the IDs of those entries into the tag parameters. It's as if you had added the IDs yourself in the tag only it's dynamically getting them for you.

Because the Ed_related class extends the EE Weblog class the 'entries' method is available and unaffected so all of the usual parameters can be used.

Usage Example

Install the module in the usual way, remembering to add the language file to your languages folder. You can use the module tag in two ways:

  1. Inside an embed template inside a weblog:entries tag
  2. Inside a template using an url segment for the parent ID or url_title

Method one requires that you use an embed because it causes parsing issues inside a weblog:entries tag. Here's a classic "books belonging to authors" example:

Basic tag example:

{exp:ed_related:entries weblog="books" parent_url_title="{embed:parent_url_title}" paginate="bottom" limit="10" orderby="cf_book_order" sort="asc"}
<h2>{title}</h2>
<p>{cf_book_isbn}</p>
{/exp:ed_related:entries}

The parent entry can be given in either a parent_id or parent_url_title parameter. How you get these IDs or url_titles is up to you and probably dependent on your situation. For example it could be via an embed variable as in the example, but could also be from an url segment, this removes the need for the tag to always be inside a weblog:entries tag.

Available Parameters and Variables

All of the parameters and variables available in the weblog:entries tag can be used and operate in exactly the same way with the addition of the parent url_title or ID:

  • parent_id="{embed:parent_id}"
  • parent_url_title="{segment_3}"

Bear in mind...

This module basically runs another weblog:entries tag so there is extra overhead to consider. That said, we have been using it in some fairly complex situations on some high traffic sites and haven't found this to be prohibitive. Make sure you use the 'disable' parameter where you can.

Get the Module

Change Log

  • v1.0.1, 3rd August 2009. Fixed incorrect assignment of child_url_title
  • v1.0.0, 29th June 2009. First release

30 Comments

  1. Toby stokes
    24/07/09 at 13:39

    Glen, does this support reverse related entries as well still, and if so, do i use
    child_id=”” (reverse)
    child_url_title=”” (reverse)
    ?

  2. Glen Swinfield
    24/07/09 at 13:53

    Glen Swinfield

    @toby - yes, forward or reverse, as you’ve guessed that’s where you use the child parameter(s) rather than parent.

    Sometimes EE relationships can be a bit confusing in terms of working out what is the parent and what is the child. If you are trying child_url_title and getting strange results just try and parent_url_title instead. One or the other will work.

  3. Toby stokes
    24/07/09 at 14:54

    Thanks!
    btw, have you any experience of using this using the playa extension for relating entries? I’m not getting any errors, but I’m not getting any results either…

  4. Glen Swinfield
    24/07/09 at 14:59

    Glen Swinfield

    yes, we seem to end up using playa for most of our sites, this module was written to give us a bit more control over related entries when using player. I haven’t tested it with playa 2 yet but it shouldn’t make a difference.

    If you’re not getting any results then check a couple of things:

    1) the module tag is in an embed, or otherwise not wrapped in a weblog entries tag.

    2) The tag has the correct weblog, status and other parameters as if you were using a weblog:entries tag.

    3) Use an if no_results block in the tag so that you can see if it is actually returning no results, or if something else is wrong.

  5. Toby Stokes
    27/07/09 at 16:41

    Hmm, I can’t seem to get anything other than no_results with any permutation of weblog, child/parent, playa/non-playa combination.

    I was getting a “PHP Warning:  Cannot modify header information” warning message, but removing a blank line at the end of mod.ed_related.php stopped that.

    Just to check, if I want replicate the standard ee tag

    {exp:weblog:entries weblog=“related”}
     

    a list of entries reverse_related to {title}

      {reverse_related_entries}
             

    {title}

      {/reverse_related_entries}
    {/exp:weblog:entries}

    I would use something like:

    {exp:ed_related:entries weblog=“related” child_url_title=”{segment_3}”}
     

    {title}

    {/exp:ed_related:entries}

    ?

  6. Toby Stokes
    27/07/09 at 16:44

    (that code got a bit garbled, i meant)


    {exp:weblog:entries weblog=“related”}
      a list of entries reverse_related to {title}:
      {reverse_related_entries}
          {title}
      {/reverse_related_entries}
    {/exp:weblog:entries}

    I would use something like:

    {exp:ed_related:entries weblog=“related” child_url_title=”{segment_3}”}
      {title}
    {/exp:ed_related:entries}

  7. Glen Swinfield
    27/07/09 at 16:53

    Glen Swinfield

    Toby can you get it to work with normal EE reverse related or related entries tags?

    In the example you have given you would need to use the weblog name of the weblog you are getting the entries from in the weblog=”” parameter.

  8. Tom Kiss
    03/08/09 at 13:37

    After banging my head for an hour trying to get it working, I noticed this error in teh codes:

    $child_url_title   = $TMPL->fetch_param( ‘child_url_title’ )    != ‘’ ? $TMPL->fetch_param( ‘parent_id’ ) : ‘’;

    :)

  9. Glen Swinfield
    03/08/09 at 13:58

    Glen Swinfield

    Thanks Tom, that’s fixed now with version 1.0.1.

    Also @Toby - I think that fixes your problem too.

  10. Toby stokes
    03/08/09 at 16:48

    That makes all the difference :)

    Thanks Tom & Glen

  11. Darren Miller
    07/08/09 at 11:13

    Looks like EE 1.6.8 has killed this. Seems like extending the weblog class is no longer allowed. A PHP 5.3 issue perhaps?

  12. Glen Swinfield
    07/08/09 at 11:15

    Glen Swinfield

    @darren - I haven’t tested on 1.6.8 yet, I’ll take a look ASAP.

  13. Darren Miller
    07/08/09 at 11:19

    Sorry, ignore me, just tried this on a “virgin” 1.6.8 install and it works fine, must be something else I’ve done…

  14. Sean
    12/08/09 at 21:15

    All I seem to ever get with this is No Results found - though using the regular reverse related entries tag I do get results.  I’ve upgraded to the latest version of both EE and this module.

    My embed tag looks like:

    {embed=“templategroup/template” child_url_title=”{segment_3}”}

    And then in templategroup/template I have:


    {exp:ed_related:entries weblog=“items” child_url_title=”{embed:child_url_title}”}
      {title}
    {if no_results} no results{/if}
    {/exp:ed_related:entries}

    Am I missing anything else?  Any help would be appreciated. thanks

  15. Glen Swinfield
    13/08/09 at 06:08

    Glen Swinfield

    Sean, it depends what you’re trying to achieve. The code you have given would list all of the entries in the ‘items’ weblog related to the entry with the url_title contained in the embed variable.

    Also have you tried outputting the embed variable in the in the template just to make sure there isn’t a problem their.

  16. Sean
    13/08/09 at 14:50

    Thanks for the info Glen. Yeah, I want to list all the entries in “items” that are related to the url_title in “departments”. 

    For example, in “departments” I have entries for - Cats, Dogs.  In “items” I have all the products that will be related to either Cats, Dogs. 

    On the “Cats” page, I want to pull in all the entries in “items” that are related to “Cats” - like so (/cats/ is segment 3 in the url):

    {exp:weblog:entries weblog=“departments”}
    {title}
    {embed=“templategroup/template” child_url_title=”{segment_3}”}
    {/exp:weblog:entries}

    And then in the embed template I have the one I posted above.

    I have tried outputting the embedded variable and it does come through (ie. cats or dogs)  - I have also tried to use child_id={segment_3} and that gives me a MYSQL error because “cats” is not a child_id (or parent_id - I tried both thinking that would get me somewhere).

    I thought I was using it properly, but I could very well be confusing the related/reverse related setup.

  17. Sean
    13/08/09 at 14:55

    I just tried it on a fresh install and I get the same results.  It’s probably my own logic that is mucking it up.

  18. Danny Tam
    26/09/09 at 07:49

    With Playa now being part of FieldFrame, will this encounter any problems with the display of related entries?

  19. Toby stokes
    28/09/09 at 09:24

    Danny, I’m using playa/fieldframe/related entries with no problems.

  20. Cecily Walker
    30/09/09 at 21:02

    How do you delete or break a relationship between entries once it has been created?

  21. Wil
    06/10/09 at 15:13

    Cheers Glen, saved me some serious query action there. Very very useful if you want the search param in related entries. Can also confirm it working with Playa 2.1, and Snow Playa.

  22. Seb Neerman
    15/11/09 at 18:59

    Just in time… as I was driving myself bonkers retrieving entry ID’s from related entries and excluding them from their reverse related counterparts, you go and publish ed_related v2 over on GitHub.

    Sadly I still can’t get it to dance. Have you tested it on Playa1.3.3? Or does it play only with Playa2.x?

  23. Glen Swinfield
    16/11/09 at 09:35

    Glen Swinfield

    Seb - it just looks up the the relationships in the exp_relationships table, so it should work for either version of playa.

    If you like, send me the template code you’re using to glen [at] erskinedesign.com and I’ll have a look.

  24. Shane Avery
    23/11/09 at 18:27

    Hello Glen,

    Thank you for providing this extension.  I am hoping your extension will help me with a dilemma, but so far no success.  I downloaded your latest version (2) from Github and installed without a hitch.

    I have a straight forward “events” and “event_dates” weblog set up for handling events on an MSM group of sites.  Templates on several different sites are pulling from these 2 weblogs on a separate site. Multiple entries in the “event_dates” weblog are related to a single entry in the “events” weblog.

    I am using the exact same example code on Github for the tags (modified for my purposes), but consistently receive “no results”.  I have looked at EE’s “Display SQL Queries?” for debugging and found this statement:

    “22
    SELECT rel_child_id, rel_parent_id FROM exp_relationships WHERE ‘3771’ IN (rel_child_id, rel_parent_id)
    23
    SELECT t.entry_id FROM exp_weblogs w, exp_weblog_titles t, exp_weblog_data d WHERE w.weblog_id = t.weblog_id AND t.entry_id = d.entry_id AND t.site_id = ‘6’ AND t.status IN (‘open’) AND w.blog_name IN(‘advertiser_entertainment_events’) AND t.entry_date < 1258999938 AND t.entry_id IN(3770,3771) AND t.entry_id != ‘3771’ “

    It appears the query is hard coded to pull from the site the tag/template exists on (t.site_id = ‘6’), whereas the entries I am trying to pull from exist on another site (t.site_id = ‘1’).  Could this be why your exp:ed_related:entries tag never returns any results in my case?  Is there a solution for this?

    Please advise.  Thanks very much for your consideration,

    Shane

  25. Shane Avery
    23/11/09 at 23:46

    Hello Glen,

    I confirmed that the exp:ed_related:entries tag used in your example works for me as designed when the template and weblog referenced in the tag reside on the same site.  It is working beautifully under those conditions.  However, when in an MSM scenario, there is no “site=” or similar parameter to tell the tag what site_id to query.  Therefore, I cannot use the tag in a template that resides on one site when the weblog it references resides on another.  May I be so bold as to request this functionality as a feature on your next release? 

    Thanks

  26. Glen
    24/11/09 at 08:44

    Glen

    Shane - sure, I can add that, it should only be a small adjustment. I’ll let you know when I get the chance.

  27. Shane Avery
    30/11/09 at 15:34

    Hi Glen,

    Hope you had a great long weekend.  I don’t want to be pushy, but I was wondering if you had a rough idea of an ETA on the above feature?

    Thank you

  28. Shane Avery
    01/12/09 at 18:53

    Hello Glen,

    Does this extension support an inclusive stack for the category parameter (i.e. category=“3&7&8”)?  It does not seem to be working for me. A regular stack works fine (i.e. category=“3|7|8”).

    Thanks

  29. Glen
    01/12/09 at 19:01

    Glen

    No it doesn’t. I am in the process of adding it, but it’s tricky.

  30. Arthur Worsley
    20/12/09 at 21:00

    Hi Glen,

    I’m having difficulty getting the child_id and child_url_title parameters to work.

    Parent_id and parent_url_title work fine and I am able to use the reverse_related_entries tag to successfully call the child related entries of the entry in question.

    I was hoping to use your plugin to overcome the limitations of being unable to sort with a pipe in the native reverse_related_entries tag.

    Any thoughts?

    Warm regards

Submit your comment

* denotes required fields

Allowed HTML

  • <a href=""></a>
  • <em></em>
  • <strong></strong>
  • <blockquote></blockquote>
  • <pre></pre>
  • <code></code>

Back to top