I'm trying to compare two entries from two different channels and see if they share a common category. Each entry can be assigned to a number of categories. Both channels use the same category group.

I want to display some information if the same category is assigned to both entries. That's the easy bit :)

How would I do this? I'll admit my brain's a bit fried this afternoon so I may well be missing the obvious solution.

2 Answers 2


exp:query would be a fairly painless way to check how many categories (if any) the two entries share. The count(*) could be changed to cp1.cat_id if you'd rather retrieve the category id from the query.

{exp:query sql="SELECT count(*) AS count FROM (SELECT cat_id FROM exp_category_posts WHERE entry_id = {first_entry_id}) cp1 INNER JOIN (SELECT cat_id FROM exp_category_posts WHERE entry_id = {first_entry_id}) cp2 ON cp1.cat_id = cp2.cat_id"}
  • Interesting. I was subconsciously trying to avoid exp:query but I think you're right in that it wouldn't be that painful.
    – foamcow
    Commented Apr 30, 2013 at 18:03
  • the performance would probably be better than any built in method of doing it, if there is one anyway. Commented Apr 30, 2013 at 18:11
  • I've realised that all I really need to do is determine if Entry A is in a particular category (category id 20). can you help me with the SQL for that? Again, I might be missing some obvious built in method for this.
    – foamcow
    Commented Apr 30, 2013 at 22:08
  • Scratch that request, it was pretty simple to do with just a straight channel:entries tag pair!
    – foamcow
    Commented Apr 30, 2013 at 22:18

It's definitely easier said than done to accomplish this. I ended up using a few different things here:

--GW Code Categories: http://gwcode.com/add-ons/gwcode-categories/documentation

This allowed me to pass the Category ID through to my listing template

--For my build some entries could have had more than one category, so I used Solspace's Primary Category (http://www.solspace.com/software/detail/primary_category/) to allow me to know which category was the main one which allowed me to pass variables down to other templates.


I'd be happy to assist you further as I'm one of the many EEers that get frustrated with the lack of Categories being part of the Publish section of EE. Thank heavens there are addons that allow us to expand on EE's limiting Category functionality.

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.