1

I'm a complete newby on Craft CMS and twig templating so please bear with me.

I currently have a users base which I want to filter by category (a custom field)

This would be my only input search element:

<select name="professionalita">
    <option value="" disabled selected>Professionalità</option>
    {% for category in craft.categories.group('professionalita') %}
    <option value="{{category.url}}">{{ category.title }}</option>
    {% endfor %}
</select>

This is what I tried to build on results.html page.

{# Get the search data #}
{% set profParam = craft.request.getParam('professionalita') %}

{% extends "_inc/layout" %}

{% block main %}
    {# Find and list matching entries #}
    {% set members = craft.users.group.('members').relatedTo(profParam) %}

    {% if members|length %}
        <ul>
            {% for member in members %}
                <li>{{ member.firstName }}</li>
            {% endfor %}
        </ul>
    {% else %}
        No members found.
    {% endif %}
{% endblock %}

I get no results even if there are actually correspondencies between the users and the category name being searched for.

1 Answer 1

1

I suggest you to read the docs about relations. You can search for relations by the following parameter

In its simplest form, you can pass in one of these things to it:

An AssetFileModel, CategoryModel, EntryModel, UserModel, or TagModel object

An element’s ID

An array of element objects and/or IDs

What you pass is a string or to be more specific a url

<option value="{{category.url}}">{{ category.title }}</option>

What you have to do is:

<option value="{{category.id}}">{{ category.title }}</option>

In order to find users related to a category id.

However I strongly suggest you to build your search with PHP if you are going to include multiple parameters (more categories/search terms/complex and+or bindings) it is going to be a mess with twig

4
  • Thanks I did mess with the value indeed. Your suggestion pointed me in the right direction.
    – Milksamsa
    Commented Dec 22, 2017 at 14:36
  • Were you able to solve the issue? Commented Dec 22, 2017 at 14:43
  • Yes I did… now I'm struggling with something different… a double http:// http:// domain.dev when calling {{ image.member }}. Craft can be daunting at beginning I guess.
    – Milksamsa
    Commented Dec 22, 2017 at 19:40
  • Well. The only difficulty is to understand the difference between an object and an array. As soon as you know that craft becomes really easy but most questions on this board are about that topic. Not sure what your member field is and why it should contain a url - you should only work with URLs in the getUrl() function - but you could create a new question or we can open a chat for further discussions/questions. Btw please have a look at this topic craftcms.stackexchange.com/a/23193/5557 Commented Dec 22, 2017 at 19:45

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.