How to get Search Results with a Relationship Field
EE 2.11.9
LS 4.4.5
I have a page with a keywords search working correctly. However on the same page I have a dropdown that selects "Focus Areas" which are relationships created in the projects channel. i.e. projects are the parents.
I've tried a number of different approaches including this one which generates no results even though there are results:
<select id="view-results" name="focus_area" onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
<option>--select--</option>
{exp:channel:entries channel='focus_areas' entry_id='not 12' orderby='title' sort='asc' disable='categories|pagination|trackbacks' dynamic='no'}
<option value="{exp:low_search:url result_page="projects/search/results" toggle:parent:project_focus_areas="{entry_id}" encode="no" reset="yes"}" {if get:focus_area == url_title} selected="selected"{/if}>{title}</option>
{/exp:channel:entries}
<option value="view-all">All</option>
</select>
An alternative approach was this one which generates the same results for all options
<select name="focus_area" id="view-results" onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
<option>--select--</option>
{exp:low_search:shortcuts group_id="1"}
<option value="{shortcut_url result_page="projects/search/results"}">{shortcut_label}</option>
{/exp:low_search:shortcuts}
</select><!-- /#select -->
My simplified search results tag is:
{exp:low_search:results query="{segment_4}" shortcut="{segment_4}" limit="10" require_query="yes"}
<h3>{title}</h3>
{/exp:low_search:results}
Replies
Low 20 Sep 2017 11:56
If your dropdown is part of a search form, then there's no need for the javascript stuff. You can populate the select with entry IDs of the related entries, like so:
Note:
- The name of the select should be child:relationship_field_name
- The values of the options should be the {entry_id}
- If you omit the value="" parameter on an option element, the value will be the text between the option tags (that's how HTML works), which most of the time is not desirable. Set to an empty string instead.
- The conditional can check for the value in the query, as per these rules
- No shortcuts needed.
- If you want to submit the form when the user selects an option in the dropdown, you can do so by adding an onchange event handler on the select and submitting the parent form when triggered.
Sean 22 Sep 2017 17:17
@low,
Sorry i didn't get back on this sooner. I've changed the search dropdown to this:
Which loads the page and I can see the url looks like domain.com/projects/search/results?child:focus_area=1365
However the search results are not actually working. I see the same results for every select option when there shoudl be either no results or only 1 (as that's all I have in test data atm)
My search results tag is:
If I use the separate search input form everything works correctly, just not the select.
Low 24 Sep 2017 18:06
What is the actual name of the Relationships field? Is it {focus_area} or is it something else?
Sean 25 Sep 2017 14:42
@low yeah it was a typo. I fixed the typo and it's working again. Apologies for taking up your time.