0

I'm working on a site that sells maps, and I'm using related_categories_mode="yes" to display similar maps to the one you're viewing. Maps can be assigned categories such as:

  • Europe
    • United Kingdom
      • England
      • Scotland
    • France
    • Germany
  • United States
    • New York ... etc ...

My problem is: If a map is marked as "Europe > United Kingdom > Scotland", EE displays any maps from "Europe", rather than "Scotland". I need it to be more specific and find ones in the highest category that's been assigned.

Does anyone know of a way to do this? I'm on EE 2.5.2.

Thanks!

3
  • 2
    Do you want to only be able to display maps from categories that have no children? e.g. would you ever show all other maps from the UK or Europe or only from England, Scotland, New York, etc.? Commented May 20, 2014 at 17:50
  • No – sometimes maps are of Europe, sometimes UK, sometimes Scotland. So I want to show ones that are of the same level in the category tree as the current one. Commented May 20, 2014 at 21:22
  • @FrankHarrison, did my updated answer helped?
    – Sobral
    Commented Jun 2, 2014 at 20:46

1 Answer 1

1

My suggestion to you is to disable the Auto-Assign Category Parents. I guess, by this way, the parent categories will not be shared between the entries assigned to their children.


If this isn't an option, let's try to use the Query module.

We need to find which categories related to the entry doesn't have children among each other.

Using the Channel module to get the values we need to:

{exp:channel:entries channel="maps" limit="1"}

    {categories backspace="1"}{category_id},{/categories}

{/exp:channel:entries}

Now, we create a query and set the parse parameter of the Channel Module to inward, so the Query module just parses after it.

{exp:channel:entries channel="maps" limit="1" parse="inward"}

{exp:query sql='SELECT c1.cat_id
        FROM exp_categories AS c1
        LEFT JOIN exp_categories AS c2
            ON c1.cat_id = c2.parent_id
            AND c2.cat_id IN ({categories backspace="1"}{category_id},{/categories})
        WHERE c2.cat_id IS NULL
            AND c1.cat_id IN ({categories backspace="1"}{category_id},{/categories})'
        backspace='1'}{cat_id}|{/exp:query}

{/exp:channel:entries}

Let's create a embed around all of this:

{exp:channel:entries channel="maps" limit="1" parse="inward"}

{embed="pages/.related_maps"
categories="{exp:query sql='SELECT c1.cat_id
        FROM exp_categories AS c1
        LEFT JOIN exp_categories AS c2
            ON c1.cat_id = c2.parent_id
            AND c2.cat_id IN ({categories backspace="1"}{category_id},{/categories})
        WHERE c2.cat_id IS NULL
            AND c1.cat_id IN ({categories backspace="1"}{category_id},{/categories})'
        backspace='1'}{cat_id}|{/exp:query}"}

{/exp:channel:entries}

Since you're using an old version of EECMS, we need to take care of white spaces.

On the embed, check for the categories variable and get the entries:

{if embed:categories}
    {exp:channel:entries channel="maps" category="{embed:categories}"}
        [...]
    {/exp:channel:entries}
{/if}

Fucking complex, right?

You can maybe have issues with the nested quotation marks. In this case, try using this heavier query:

{exp:channel:entries channel="maps" limit="1" parse="inward"}

{embed="pages/.related_maps"
categories="{exp:query sql='SELECT c1.cat_id
        FROM exp_categories AS c1
        LEFT JOIN exp_categories AS c2
            ON c1.cat_id = c2.parent_id
            AND c2.cat_id IN (
                    SELECT cat_id
                    FROM exp_category_posts
                    WHERE entry_id = {entry_id}
                )
        INNER JOIN exp_category_posts AS cp
            ON cp.cat_id = c1.cat_id
        WHERE c2.cat_id IS NULL
            AND cp.entry_id = {entry_id}'
        backspace='1'}{cat_id}|{/exp:query}"}

{/exp:channel:entries}

To prevent this parsing issues, I suggest you to update EECMS to a newer version.

2
  • That's a good idea but unfortunately I need the auto-assign to be on: when you're looking at maps of Europe you also want to see all of the sub-categories mixed in... Commented May 30, 2014 at 1:22
  • @FrankHarrison, please, check if my update helps.
    – Sobral
    Commented Jun 1, 2014 at 18:58

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.