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.