All Low add-ons are now owned by EEHarbor. Read the blog post.

Support archive

Using Playa child fields in a search form

Stephen 27 Feb 2013 15:25 question, complete

Hi Low,

I've got an Events channel and an Images channel. The Images channel has a single select Playa field which links an image to an event.

I've got an Image search form, and the client is asking if it's possible to pull search fields from the Events channel (the child entry in Playa terms, I think!) into this search form.

Is this possible at all? Here's what I was trying, which didn't work (it just told me there were no results when I entered anything into this field):


<label>Event Custom Field Test</label>
<input type="text" name="child:cf_event_test_field" value="" />


Thanks,

Stephen

Replies

  1. Low 27 Feb 2013 15:33

    Two options here.

    The option child:cf_playa_field expects an entry_id as value, so you'd have to create a <select>-field where you can select the child entry. Results will then be filtered by entries that have the selected entry as a child.

    The option search:cf_playa_field can also work, as Playa stores data in the native exp_channel_data table like this:

    [123] entry-url-title Entry Url Title


    ...so you can search through the child entry's titles only.

    Because Playa saves its data like that, the Playa field itself can also be weighted and added to the search index, so you could even use keywords.

  2. Stephen 27 Feb 2013 16:19

    Hi Low,

    Thanks for going through what is currently possible. I guess this is how I got the following working yesterday in another search form (for Events), as it looks through the title field.


    <label><input type="checkbox" name="parent:cf_gallery_event[]" value="not EMPTY" /> Images attached</label>


    I guess this post has turned into a feature request, as I'd love to be able to filter results by the custom fields in the parent or child entries too. Is this something you can see coming in the future? Or is is a massive ball-ache to go down that route?

    Thanks,

    Ste

  3. Low 28 Feb 2013 08:29

    I think you're mistaken the constant EMPTY with IS_EMPTY there (see the native docs on that).

    As for your feature request -- I'll consider it, but I doubt it'll make it to the core. I've already thought long and hard about adding related entries to the search index, but that opens up a big can of worms, especially if the related entry in turn has a 3rd tier relationship.

    Now, searching fields directly does simplify things somewhat, but still. Ball-ache sounds about right.

    It could always be done as an add-on for Low Search. :)

  4. Stephen 1 Mar 2013 04:26

    Hi Low,

    Thanks for the IS_EMPTY pointer - I'll fix that.

    Yes, it's mainly the searching directly (rather than keyword search indexing) that I'm after. I've told the client it isn't possible (other than the title searches/filtering), but I reckon it'd be a good feature to add to the core if you've ever got 5 minutes to spare one afternoon! ;-)

    Thanks,

    Ste

  5. Low 1 Mar 2013 12:28

    Also note that if you'd want to search the relationship field specifically, try using search: instead of parent:.

    <label><input type="checkbox" name="search:cf_gallery_event" value="not EMPTY" /> Images attached</label>

    Stephen 17 Jun 2013 03:04

    Should the code sample above be 'not IS_EMPTY'? Just double-checking if you copy and pasted my error ridden code above from before you corrected me. :-)

    Low 17 Jun 2013 06:41

    You are correct!

  6. Stephen 17 Jun 2013 03:05

    Hi Low,

    I'm just running a search on this Events channel again, and trying to filter the results to those events that have had an image uploaded and linked to them via the Playa field in the Images channel.

    I thought the search was working fine, but I've just noticed that I'm only getting 398 results, when I should be getting 553 results. Do you have any ideas from the code sample below why all the results aren't being brought show in my search results?

     
    {exp:low_search:form collection="lv_event_image_search" search_mode="all" result_page="/searches/event-image-search-results" secure="no" query="{segment_3}"}

    <input type="hidden" name="parent:cf_gallery_event[]" value="not IS_EMPTY" />
    <input type="hidden" name="status" value="not xxx" />

    <p><input type="submit" value="Search" /></p>

    {/exp:low_search:form}


    Running the following query on the database for the Images channel is giving me 553 results (field_id_94 is the Playa field), which also tallies up with the results I'm getting in the Edit page of the CP.

     
    SELECT entry_id, field_id_94, field_id_100 FROM exp_channel_data WHERE channel_id = 7 AND field_id_94 != '';


    Thanks,

    Stephen

    Stephen 17 Jun 2013 03:23

    Hi Low,

    In fact, I know what the issue is now. Some events have more than one image entry attached to them. So if I search via the Images channel, I get all the results; whilst if I search via the Events channel, I get less results due to some events having multiple images only counting as one result.

    I don't think there's any way around this, unless I could search on related fields and make this a search of the Image channel again; but that brings us back to square one. :-)

    Thanks,

    Stephen

    Low 17 Jun 2013 06:42

    Also, using the parent:field or child:field parameters, the IS_EMPTY syntax won't work. It only applies to search:field parameters.

    Stephen 17 Jun 2013 11:08

    Hi Low,

    You say this, but in the example I posted above last night, I find that using the parent:field with 'not IS_EMPTY' gives the results that I would expect. So are you sure about this not working? Or are you talking about a different situation completely?

    Thanks,

    Stephen

    Low 17 Jun 2013 11:58

    Yes, sure. Check your Output Profiler. You can see a query like this:

    SELECT 'child_id' AS entry_id 
    FROM ('exp_relationships')
    WHERE 'parent_id' NOT IN ('IS_EMPTY')


    ...or the Playa equivalent. Note how the IS_EMPTY is used as a literal string. It's meant for search:-params only.

    Stephen 8 Jul 2013 13:35

    Hi Low,

    Things get a bit too technical for me to understand once we start checking the output profiler. :-) But in real world examples, I'm finding the following:

    I'm searching in a Contacts channel, and the Playa field is in an Orders channel (this Playa field pulls in contacts).

    The following works and gives me 300 results out of a channel of 3,000, which is what I expect:

     
    <p><label><input type="checkbox" name="parent:cf_order_organiser" value="not IS_EMPTY" /> Has order</label></p>


    The following doesn't work - it gives me 0 results, when I'd be hoping to see 2,700 results (3,000 - 300):

     
    <p><label><input type="checkbox" name="parent:cf_order_organiser" value="IS_EMPTY" /> Has order</label></p>


    Another reason I'm following this up is that I'd really like to get the second example (value="IS_EMPTY") working, so wondered if you had any alternative ideas for this?

    Thanks,

    Stephen

    Low 8 Jul 2013 13:42

    As I said before:

    Also, using the parent:field or child:field parameters, the IS_EMPTY syntax won't work. It only applies to search:field parameters.


    That means you can't use IS_EMPTY in any parent:field or child:field parameter. You can only use it in a search:field parameter. Try if name="search:cf_order_organiser" works for you.

    Stephen 8 Jul 2013 14:17

    When you said I can't use "IS_EMPTY", do you specifically mean, "IS_EMPTY", as opposed to "not IS_EMPTY" ? Or are you saying that both shouldn't work? This is where my confusion is coming from. As I took you to mean both, but was finding that "not IS_EMPTY" was working.

    Stephen 8 Jul 2013 14:26

    Also, name="search:cf_order_organiser" doesn't seem to work as this search is for the Contacts channel rather than the Orders channel. Thanks for the suggestion though.

  7. Low 8 Jul 2013 14:43

    Okay, so. Here's the thing.

    You can only use the IS_EMPTY string effectively in search:field parameters. This includes "not IS_EMPTY" and all other uses. The string is used in native search-parameters, and will be used literally in any other field, which you can see in queries in the output profiler.

    I think you want to search for Contacts that have been selected in any of the Orders channel's entries, correct? If so, there is currently no way to do that with Low Search, I'm afraid.