Get saved sub category on user I have my users setup with a category field that links to a category of countries and each country has states/provinces.

I have my form setup to only display the states listed under United States. The plugin I created saves this data as expected.

The problem I'm having is accessing the state which was saved for the user. This is my current code. It works BUT there has to be a better way:

{% if user.addressMatrix | length %}

    {% for addressInfo in user.addressMatrix %}

        {% set cat = addressInfo.region[1] %}

        {{ cat.title }}

   {% endfor %}

{% endif %}


Please advise and thanks in advance.
Ok so I figured out the issue. I had the category field setup to have a limit of 1, which I assumed would be ok. My thinking was that you're only choosing 1 state. But because state is a sub category of country I need to allow for country and state AKA a limit of 2. Once I did this I was able to structure my template accordingly:

{% if user.addressMatrix | length %}

    {% for addressInfo in user.addressMatrix %}

         {% if addressInfo.type == "address" %}

            {% for item in addressInfo.region %}

                {% if item.level == 2 %}

                {{ item.title }}

                {% endif %}

            {% endfor %}

        {% endif %}

   {% endfor %}

{% endif %}


I think that future releases of Craft ought to have more options for category fields, like: Restricting selections to a sub category, limiting the number of sub categories.