0

I'm using EE 7.3.9 have 2 channels related via a relationship field, and I would like to create a page showing all the entries in channel 1 (the parent) that have in common the same field in the channel 2 (the child).

So I created a list with

{exp:channel:entries channel="channel2" url_title="{segment_2}" limit="1"}
{parents field="relationship_field" limit="4" paginate="bottom"}
{parents:title}
{paginate}
        <p>Page {current_page} of {total_pages} pages {pagination_links}</p>
{/paginate}
{/parents}
{/exp:channel:entries}

It shows the first 4 entries that have that particular field in {segment_2}, but the pagination doesn't show.

I also tried to achieve the same result in the other way, accessing via the parent field

{exp:channel:entries channel="channel1" search:relationship_field="{segment_2}" limit="4" paginate="bottom"}

but it doesn't show anything, and in the documentation there is this statement

"Relationship fields cannot be searched using the search parameter".

So, how can I achieve the goal to show all the entries that have in common a particular relationship field AND the pagination?

Thank you in advance for your answer.

1

1 Answer 1

0

This might be a good excuse to use a custom query combined with a template embed. In short, you use the query to get a list of entry_ids based on your criteria. Then pass it to the embed template to output and paginate:

First the custom query

{!-- get related parent entry IDs for the given child entry --}
{exp:query sql="SELECT parent_id 
               FROM exp_relationships 
               WHERE child_id = 
                 (SELECT entry_id FROM exp_channel_titles WHERE url_title = '{segment_2}' LIMIT 1)"}

    {!-- if it's the first row, set a variable with the entry ID. For subsequent rows, append the ID with a pipe. --}
    {if count == 1}{embed="your_template_group/related_entries" the_ids="{parent_id}"}{if:else}|{parent_id}{/if}
{/exp:query}

and the template embed:

{exp:channel:entries channel="channel1" entry_id="{embed:the_ids}" limit="4" paginate="bottom"}
    {title}
    {paginate}
        <p>Page {current_page} of {total_pages} pages {pagination_links}</p>
    {/paginate}
{/exp:channel:entries}
1
  • Wow, that's a very smart solution, I'd never thought to SQL queries. I tried to implement your code in my page, with these corrections: {if count == 1}{embed="template_group/related_entries" the_ids="{parent_id}"}{if:else}{embed="template_group/related entries" the_ids="|{parent_id}"}{/if} and adding dynamic="no" in the embed template. It shows effectively the entries related to the {segment_2} child (as my code) but.. no pagination. Also, the limit parameter doesn't work with this method, no matter if I put 2 or 4 or whatever I see all the filtered entries.
    – Elena
    Commented Aug 30, 2023 at 15:11

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.