1

I have a request to extend an existing "advanced search" form (multiple fields, including keyword and a categories dropdown) so that entries with a category that matches the keyword input are returned.

For example, let's say my entries are all book authors, and among various entry fields like their bio and their published books, there's also a Topic category, and authors may be associated with 0-n categories.

And let's say I have an author Jane Smith, and one of her selected Topic categories is Poetry, but for some reason the word "poetry" does not exist in her bio or any of her book titles or for that matter anywhere at all in her main entry, but rather only as that category selection.

How would I modify my search so that anyone typing "poetry" into the keyword search field (rather than picking it from the Topics dropdown menu field) will get back Jane Smith as one of their results?

Still pretty new to Craft, but I feel like there has to be some way. If not via clever query construction on the results template, then maybe by some kind of way to automatically replicate the text of the author's topic categories into an entry field that's never displayed to users, just only exists to get that text into a place where it's picked up by the search index? Would be grateful for any thoughts. Thanks.

Existing search (this is followed by a bunch more Twig that merges these results with results of other entry types but I don't know that any of that is relevant here):

{% set profileParams = {
    section: 'profiles',
    search: {
        query: 'title:' ~ query ~ ' OR ' ~ 
                     'position:' ~ query ~ ' OR ' ~ 
                     'jobTitle:' ~ query ~ ' OR ' ~ 
                     'institution:' ~ query,
        subLeft: true,
        subRight: true
    },
    order: 'score',
    limit: null
} %}

[...]

{% set profileEntries = craft.entries( profileParams ) %}
1

It's hard to say how you could include it since we don't know the rest of your search. Generally the best idea to search is with php and not with twig but you could grab all categories with the title (or whatever field it should be) with

{% set categoryIds = craft.categories.title("Your search term").ids() %}

Then you can fetch all entries that are related to these categories with

{% set entries = craft.entries.relatedTo(categoryIds).find() %}

However: if you just follow this example category titles are included in your search.

  • oh no, I left off my actual code, sorry. have edited to add. it's specifying particular fields, instead of the simpler example you shared, which I guess excludes the category titles, and since they're off in a whole other table in the database I don't know how to pull them in. But will look at your first thought on this as well. thank you! – Sapphireblue Feb 20 '18 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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