<p>Please see <a href="https://craftcms.stackexchange.com/questions/5107/users-relatedto-multiple-categories/5108#5108">Users relatedTo multiple categories</a> for improved answer since this questions details are outdated.</p>

CC BY-SA 3.0
2014-12-06T11:13:40.997Z
2014-12-06T11:13:40.997Z
2017-04-13T12:51:00.303Z
Sean Delaney
<p>I'm building a Users search page.</p>

<p>I want to search the members group based on the following fields that I've setup for Users:</p>

<pre><code>First Name
Last Name
Company Name (handle: genericCompany, type: Plain Text)
Company Overview (handle: membersCompanyOverview, type: Rich Text)
Contact (handle: membersContact, type: Plain Text)
Contact Email Address (handle: globalEmailAddress, type: Email Address)
Contact Position (handle: genericPosition, type: Plain Text)
Services (handle: membersServices, type: Categories)
Locations (handle: membersLocations, type: Categories)
</code></pre>

<p>My HTML form (removed labels etc to keep form example clean):</p>

<pre><code>&lt;form id="directory_search_form"&gt;
    &lt;input type="text" name="keywords" value=""&gt;
    &lt;input type="checkbox" name="services[]" value="service-1"&gt;
    &lt;input type="checkbox" name="services[]" value="service-2"&gt;
    &lt;input type="checkbox" name="services[]" value="service-3"&gt;
    &lt;input type="checkbox" name="locations[]" value="location-1"&gt;
    &lt;input type="checkbox" name="locations[]" value="location-2"&gt;
    &lt;button type="submit"&gt;Go&lt;/button&gt;
&lt;/form&gt;
</code></pre>

<p>My jQuery:</p>

<pre><code>var form = $("#directory_search_form");

form.on("submit",function(event) {
    event.preventDefault();

    var errors = 0;

    if(!$("#keywords").val()) {
        errors++;
    }

    if(errors == 0) {
        $.post("/search",$(this).serialize()).done(function(result) {
            $("#results").html(result);
        });
    } else {
        alert("Please enter some keywords and try again.");

        return false;
    }
});
</code></pre>

<p>My Search template:</p>

<pre><code>{% if craft.request.isAjax %}
    {% set params = craft.request.getPost() %}
    {% set keywords = "" %}
    {% set serviceCategories = craft.categories.group("services") %}
    {% set locationCategories = craft.categories.group("locations") %}
    {% for key in params | keys %}
        {% if key == "keywords" %}
            {% set keywords = params["keywords"] %}
            {% set keywords = keywords | trim %}
        {% endif %}
        {% if key == "services" %}
            {% set services = params["services"] %}
            {% set servicesCategories = services | join(", ") | trim %}
            {% set serviceCategories = craft.categories.group("services").slug(servicesCategories) %}
        {% endif %}
        {% if key == "locations" %}
            {% set locations = params["locations"] %}
            {% set locationsCategories = locations | join(", ") | trim %}
            {% set locationCategories = craft.categories.group("locations").slug(locationsCategories) %}
        {% endif %}
    {% endfor %}
    {% set members = craft.users.group("members").status("live").relatedTo([ "and", { targetElement: serviceCategories }, { targetElement: locationCategories } ]) %}
    {% if members | length %}
        {% for member in members %}
            {{ member.email }}&lt;br&gt;
        {% endfor %}
    {% endif %}
{% endif %}
</code></pre>

<p>This gives me all members for selected categories.  However I want to hook up the keywords field to further search users. </p>

<p>I also want to get only those users that are related in all categories not either categories.</p>

<p>I've got the keywords but if I add </p>

<pre><code>.search("*" ~ keywords ~ "*")
</code></pre>

<p>I don't get any members for my keywords - e.g Operations Manager when there are Members in the system with Contact Position field set to Operations Manager.</p>

<p>I want to search by multiple keywords and multiple categories.</p>

<p>What am I doing wrong?</p>

CC BY-SA 3.0
2014-12-05T12:16:52.927Z
Sean, I don't see what's wrong with your search parameter. What does `{{ keywords }}` print? And what exactly are you trying to achieve with the asterisks that you concatenate to your string? You know that this only adds it to the first an the last word, right?
carlcs
CC BY-SA 3.0
2014-12-05T12:17:53.690Z
if I input "Operations Manager" as keywords, I get "Operations Manager" printed out.
Sean Delaney
CC BY-SA 3.0
2014-12-05T12:19:41.337Z
I added asterisks as per http://buildwithcraft.com/docs/searching - return any users that contains the keywords. I guess by your comments I'm using this wrong?
Sean Delaney
CC BY-SA 3.0
2014-12-05T12:22:35.350Z
Not wrong, but you this doesn't do `'*Operations* *Manager*'`, but `'*Operations Manager*'` instead.
carlcs
CC BY-SA 3.0
2014-12-05T12:24:02.143Z
What does this return for you: `{% set members = craft.users.search('Operations Manager') %}`? That's what I'd do, remove all potential troublemakers...
carlcs
CC BY-SA 3.0
2014-12-05T12:26:02.477Z
I get all the users with Company Positions to set Operations Manager.
Sean Delaney
CC BY-SA 3.0
2014-12-05T12:26:30.907Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/19201/discussion-between-carlcs-and-sean-delaney).
carlcs
CC BY-SA 3.0
2014-12-05T12:28:43.873Z
I want to be able to search for "Operation" and get back Operations...
Sean Delaney
CC BY-SA 3.0
2014-12-05T13:48:18.650Z
Sean, let's close this question. I think my answer / solution doesn't fit the original code (you changed it a lot) and I also posted the relevant parts of my answer [here](http://craftcms.stackexchange.com/a/5108/125)!
carlcs
<users><field><search><ajax><custom-field>
Searching members fields
# Searching members fields

(Asked by: username_0 on 2014-12-05)

I'm building a Users search page.
I want to search the members group based on the following fields that I've setup for Users:
First Name
Last Name
Company Name (handle: genericCompany, type: Plain Text)
Company Overview (handle: membersCompanyOverview, type: Rich Text)
Contact (handle: membersContact, type: Plain Text)
Contact Email Address (handle: globalEmailAddress, type: Email Address)
Contact Position (handle: genericPosition, type: Plain Text)
Services (handle: membersServices, type: Categories)
Locations (handle: membersLocations, type: Categories)

My HTML form (removed labels etc to keep form example clean):

    
    
    
    
    
    
    Go


My jQuery:
var form = $("#directory_search_form");

form.on("submit",function(event) {
    event.preventDefault();

    var errors = 0;

    if(!$("#keywords").val()) {
        errors++;
    }

    if(errors == 0) {
        $.post("/search",$(this).serialize()).done(function(result) {
            $("#results").html(result);
        });
    } else {
        alert("Please enter some keywords and try again.");

        return false;
    }
});

My Search template:
{% if craft.request.isAjax %}
    {% set params = craft.request.getPost() %}
    {% set keywords = "" %}
    {% set serviceCategories = craft.categories.group("services") %}
    {% set locationCategories = craft.categories.group("locations") %}
    {% for key in params | keys %}
        {% if key == "keywords" %}
            {% set keywords = params["keywords"] %}
            {% set keywords = keywords | trim %}
        {% endif %}
        {% if key == "services" %}
            {% set services = params["services"] %}
            {% set servicesCategories = services | join(", ") | trim %}
            {% set serviceCategories = craft.categories.group("services").slug(servicesCategories) %}
        {% endif %}
        {% if key == "locations" %}
            {% set locations = params["locations"] %}
            {% set locationsCategories = locations | join(", ") | trim %}
            {% set locationCategories = craft.categories.group("locations").slug(locationsCategories) %}
        {% endif %}
    {% endfor %}
    {% set members = craft.users.group("members").status("live").relatedTo([ "and", { targetElement: serviceCategories }, { targetElement: locationCategories } ]) %}
    {% if members | length %}
        {% for member in members %}
            {{ member.email }}
        {% endfor %}
    {% endif %}
{% endif %}

This gives me all members for selected categories.  However I want to hook up the keywords field to further search users. 
I also want to get only those users that are related in all categories not either categories.
I've got the keywords but if I add 
.search("*" ~ keywords ~ "*")

I don't get any members for my keywords - e.g Operations Manager when there are Members in the system with Contact Position field set to Operations Manager.
I want to search by multiple keywords and multiple categories.
What am I doing wrong?

**Tags:** users, field, search, ajax, custom-field

> **Comments:**
> **username_1**: Sean, I don't see what's wrong with your search parameter. What does `{{ keywords }}` print? And what exactly are you trying to achieve with the asterisks that you concatenate to your string? You know that this only adds it to the first an the last word, right?  
> **username_0**: if I input "Operations Manager" as keywords, I get "Operations Manager" printed out.  
> **username_0**: I added asterisks as per http://buildwithcraft.com/docs/searching - return any users that contains the keywords. I guess by your comments I'm using this wrong?  
> **username_1**: Not wrong, but you this doesn't do `'*Operations* *Manager*'`, but `'*Operations Manager*'` instead.  
> **username_1**: What does this return for you: `{% set members = craft.users.search('Operations Manager') %}`? That's what I'd do, remove all potential troublemakers...  
> **username_0**: I get all the users with Company Positions to set Operations Manager.  
> **username_1**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/19201/discussion-between-username_1-and-sean-delaney).  
> **username_0**: I want to be able to search for "Operation" and get back Operations...  
> **username_1**: Sean, let's close this question. I think my answer / solution doesn't fit the original code (you changed it a lot) and I also posted the relevant parts of my answer [here](http://craftcms.stackexchange.com/a/5108/125)!  

---

## 1 answers

### Answer by username_0 on 2014-12-06. Score: 0 | Accepted answer

Please see [Users relatedTo multiple categories](https://craftcms.stackexchange.com/questions/5107/users-relatedto-multiple-categories/5108#5108) for improved answer since this questions details are outdated.


---

