Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpressionEngine: Help w/ Ordering Reverse Related Entries

I've run into some problems with the sorting of some reverse related entries. There's just some limitations with EE and I'm looking for a quick solution. Any help you can provide will be appreciated.

I have two channels: registrations and students. Students has a relationship field that links each entry to an entry in the registrations channel. (I need to continue to use the EE relationships field.)

The students channel has two category groups assigned to it: grades (group #1) and instrument (group #2). Category IDs #1-6 are in the grades category.

The following code achieves half of what I need it to do:

{exp:channel:entries channel="registrations" entry_id="{segment_4}" dynamic="no"}
<table>
{reverse_related_entries channel="students"}
{categories show="1|2"}
 <tr>
  <td><?php print $count; ?>.</td>
  <td>{title}</td>
  {embed="_includes/student_print" student_id="{entry_id}"}
 </tr>
{/categories}
{/reverse_related_entries}
</table>
<table>
{reverse_related_entries channel="students"}
{categories show="3|4"}
 <tr>
  <td><?php print $count; ?>.</td>
  <td>{title}</td>
  {embed="_includes/student_print" student_id="{entry_id}"}
 </tr>
{/categories}
{/reverse_related_entries}
</table>
<table>
{reverse_related_entries channel="students"}
{categories show="5|6"}
 <tr>
  <td><?php print $count; ?>.</td>
  <td>{title}</td>
  {embed="_includes/student_print" student_id="{entry_id}"}
 </tr>
{/categories}
{/reverse_related_entries}
</table>
{/exp:channel:entries}

Here's the student_print embed:

{exp:channel:entries channel="students" entry_id="{embed:student_id}" dynamic="no"}
<td><font size="2">{categories show_group="2"}{category_name}{/categories}</font></td>
<td><font size="2">{categories show_group="1"}{category_name}{/categories}</font></td>
{/exp:channel:entries}

Now -- what I need it to do is order the reverse related entries by the CUSTOM ORDER of the categories in the instruments category group (group #2). I just didn't know how to go about doing what I'm currently doing (displaying the three tables -- each displaying entries from specific categories in group #1) and putting them in the custom order of the categories in group #2.

Again -- the categories in group #2 are in a custom order and I need to display the related entries in that custom order. This is important.

Any ideas? Can this be done with a custom query? I would really appreciate a code example -- if possible. This is stretching my EE and SQL chops as it is.

Thank so you much for your time.

like image 276
Mathew Smith Avatar asked Oct 16 '12 05:10

Mathew Smith


Video Answer


2 Answers

I really don't use native relationships any more, too buggy for me. so playa would be better. however, i found this bug, that might be related to your issue: http://expressionengine.com/bug_tracker/bug/16373

like image 193
Jelle Dijkstra Avatar answered Sep 23 '22 02:09

Jelle Dijkstra


To be honest, my first impression is that this is the wrong way to structure this. Too complex. But that's always easy to say coming from the outside right? :)

Second, I'm not sure what you need this Registrations channel for, since it appears as though you're not using any data from it (unless you've excised some bits of your code for clarity)?

But regardless, here is some untested code. The gist is that you can only order by category by using the channel:categories tag, and then passing the {category_id} to a new channel:entries tag. If we pass a list of all the entry_ids to each tag within, it should filter out all that do not belong to the specific {category_id} you pass.

{exp:channel:entries channel="registrations" entry_id="{segment_4}" dynamic="no"}
    {embed="_includes/student_print" entry_ids="{reverse_related_entries channel="students" backspace="1"}{entry_id}|{/reverse_related_entries}"}
{/exp:channel:entries}

Then your embed looks like this:

{exp:channel:categories channel="students" style="linear" show="1|2"}
{if count == "1"}<table>{/if}
    {exp:channel:entries channel="students" entry_id="{embed:entry_ids}" category="{category_id}" dynamic="no"}
    <tr>
        <td><?php print $count; ?>.</td>
        <td>{title}</td>
        <td><font size="2">{categories show_group="2"}{category_name}{/categories}</font></td>
        <td><font size="2">{categories show_group="1"}{category_name}{/categories}</font></td>      
    </tr>
    {/exp:channel:entries}
{if count == total_results}</table>{/if}
{/exp:channel:categories}

{exp:channel:categories channel="students" style="linear" show="3|4"}
{if count == "1"}<table>{/if}
    {exp:channel:entries channel="students" entry_id="{embed:entry_ids}" category="{category_id}" dynamic="no"}
    <tr>
        <td><?php print $count; ?>.</td>
        <td>{title}</td>
        <td><font size="2">{categories show_group="2"}{category_name}{/categories}</font></td>
        <td><font size="2">{categories show_group="1"}{category_name}{/categories}</font></td>      
    </tr>
    {/exp:channel:entries}
{if count == total_results}</table>{/if}
{/exp:channel:categories}

{exp:channel:categories channel="students" style="linear" show="5|6"}
{if count == "1"}<table>{/if}
    {exp:channel:entries channel="students" entry_id="{embed:entry_ids}" category="{category_id}" dynamic="no"}
    <tr>
        <td><?php print $count; ?>.</td>
        <td>{title}</td>
        <td><font size="2">{categories show_group="2"}{category_name}{/categories}</font></td>
        <td><font size="2">{categories show_group="1"}{category_name}{/categories}</font></td>      
    </tr>
    {/exp:channel:entries}
{if count == total_results}</table>{/if}
{/exp:channel:categories}

I don't know what this $count PHP variable you have in there is, but that may need modification since it's within the embed now.

Let me know if this bears any fruit?

like image 27
Derek Hogue Avatar answered Sep 26 '22 02:09

Derek Hogue