0

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?

9
  • 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
    Dec 5 '14 at 12:16
  • if I input "Operations Manager" as keywords, I get "Operations Manager" printed out. Dec 5 '14 at 12:17
  • I added asterisks as per buildwithcraft.com/docs/searching - return any users that contains the keywords. I guess by your comments I'm using this wrong? Dec 5 '14 at 12:19
  • Not wrong, but you this doesn't do '*Operations* *Manager*', but '*Operations Manager*' instead.
    – carlcs
    Dec 5 '14 at 12:22
  • What does this return for you: {% set members = craft.users.search('Operations Manager') %}? That's what I'd do, remove all potential troublemakers...
    – carlcs
    Dec 5 '14 at 12:24
0

Please see Users relatedTo multiple categories for improved answer since this questions details are outdated.

Not the answer you're looking for? Browse other questions tagged or ask your own question.