Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T11:13:40.997Z
Answers.LastActivityDate: 2014-12-06T11:13:40.997Z
Answers.LastEditDate: 2017-04-13T12:51:00.303Z
Answers.UserDisplayName: Sean Delaney
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-05T12:16:52.927Z
Comments.Text: 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?
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-05T12:17:53.690Z
Comments.Text: if I input "Operations Manager" as keywords, I get "Operations Manager" printed out.
Comments.UserDisplayName: Sean Delaney
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-05T12:19:41.337Z
Comments.Text: 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?
Comments.UserDisplayName: Sean Delaney
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-05T12:22:35.350Z
Comments.Text: Not wrong, but you this doesn't do `'*Operations* *Manager*'`, but `'*Operations Manager*'` instead.
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-05T12:24:02.143Z
Comments.Text: What does this return for you: `{% set members = craft.users.search('Operations Manager') %}`? That's what I'd do, remove all potential troublemakers...
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-05T12:26:02.477Z
Comments.Text: I get all the users with Company Positions to set Operations Manager.
Comments.UserDisplayName: Sean Delaney
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-05T12:26:30.907Z
Comments.Text: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/19201/discussion-between-carlcs-and-sean-delaney).
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-05T12:28:43.873Z
Comments.Text: I want to be able to search for "Operation" and get back Operations...
Comments.UserDisplayName: Sean Delaney
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-05T13:48:18.650Z
Comments.Text: 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)!
Comments.UserDisplayName: carlcs
Tags: <users><field><search><ajax><custom-field>
Title: Searching members fields
ThreadText: # 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):
  
  ```
  <form id="directory_search_form">
      <input type="text" name="keywords" value="">
      <input type="checkbox" name="services[]" value="service-1">
      <input type="checkbox" name="services[]" value="service-2">
      <input type="checkbox" name="services[]" value="service-3">
      <input type="checkbox" name="locations[]" value="location-1">
      <input type="checkbox" name="locations[]" value="location-2">
      <button type="submit">Go</button>
  </form>
  
  ```
  
  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 }}<br>
          {% 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**: username_0, 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-username_0-username_0).  
  > **username_0**: I want to be able to search for "Operation" and get back Operations...  
  > **username_1**: username_0, 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.
  
  
  ---
  
  