Search code examples
expressionengine

How to paginate ExpressionEngine {reverse_related_entries}


I'm surprised I haven't run into this before, or at least I forgot how I got it working.

I have a standard EE relationship field. In this case, I have a "Products" channel, and each product belongs to an entry in the "Designers" channel.

I quite obviously want a page to list all products for a single designer. My first attempt looked like this:

{exp:channel:entries channel="designers" url_title="{segment_3}" limit="1"}

    {reverse_related_entries channel="products" limit="15"}
        {snippet_product_grid}
        {snippet_paginate}
    {/reverse_related_entries}

{/exp:channel:entries}

According to anecdotal evidence in the EE documentation, you can't paginate reverse related entries. So what's the solution? Is there a way to easily grab all the product entry IDs, given the url_title of a designer? Or do I have to resort to using SQL?

Is there any way I could solve this using the Stash add-on?


Solution

  • What janvl said with playa. Using stash, you want to use the dev branch to which Mark Croxton has added a pagination feature for {exp:stash:get_list}

    https://github.com/croxton/Stash/tree/dev#expstashget_list-tag-pair

    Haven't tested this code on a local sandbox but the code below should get you started

    {exp:channel:entries channel="designers" url_title="{segment_3}" limit="1"}
        {exp:stash:set_list name="related_products" parse_tags="yes"}
            {reverse_related_entries channel="products"}
                {stash:st_item_title}{title}{/stash:st_item_title}
            {/reverse_related_entries}
        {/exp:stash:set_list}
    {/exp:channel:entries}
    
    
    {exp:stash:get_list name="related_products" parse_tags="yes" parse_conditionals="yes" prefix="my_prefix" paginate="bottom"}
    
        {if my_prefix:count == 1}<ul>{/if}
            <li>{st_item_title}</li>
        {if my_prefix:count == my_prefix:total_results}</ul>{/if}
    
        {if my_prefix:no_results}
            <p>No related products</a></p>
        {/if}
    
        {my_prefix:paginate}
            {pagination_links}
                <ul>
                    {first_page}
                            <li><a href="{pagination_url}" class="page-first">First Page</a></li>
                    {/first_page}
    
                    {previous_page}
                            <li><a href="{pagination_url}" class="page-previous">Previous Page</a></li>
                    {/previous_page}
    
                    {page}
                            <li><a href="{pagination_url}" class="page-{pagination_page_number} {if current_page}active{/if}">{pagination_page_number}</a></li>
                    {/page}
    
                    {next_page}
                            <li><a href="{pagination_url}" class="page-next">Next Page</a></li>
                    {/next_page}
    
                    {last_page}
                            <li><a href="{pagination_url}" class="page-last">Last Page</a></li>
                    {/last_page}
                </ul>
            {/pagination_links}
        {/my_prefix:paginate}
    
    {/exp:stash:get_list}