1

I'm able to get a simple search:custom_field="something" working fine after reading the docs. However, I'd like it to search three fields to see if the parameter passed through segment_3 is met and then display the results.

Something like this:

{exp:channel:entries 
    channel="products"
    dynamic="no" 
    search:product_category="{segment_3}" OR
    search:product_by_colour="{segment_3}" OR
    search:product_by_collection="{segment_3}"
    search:product_for_sale="y"
    search:product_popup_only="=not y"
    orderby="product_price|product_sku" sort="asc|asc"
}

Thanks for your help.

2
  • If you use a single segment as a search parameter for multiple fields isn't that likely to introduce issues down the line? What if you have duplicate category and collection names? How would the site know which field to filter by?
    – Dom Stubbs
    Nov 27, 2012 at 10:49
  • those fields are drawn from a channel of their own and the client will only have one of each thing in those channels. Nov 27, 2012 at 21:46

4 Answers 4

8

You cannot use OR clauses inside of a {exp:channel:entries} tag. The best way to handle this would be to write a custom plugin that would do the selection, or use the Active Record plugin.

You should not use the built-in Query plugin because it unsafe to pass user-submitted values (such as {segment_*} variables) to this plugin, while the Active Record plugin does escape any of these values for you.

2

I believe Solspace Super Search may be able to handle this as well. You may want to take a peek!

2
  • I can vouch for Super Search, it makes these things pretty easy, and by the sounds of it your whole site may benefit from it (or Low Search, but I haven't tried that one). Nov 27, 2012 at 11:03
  • Super Search works, and I can also vouch for (and highly recommend) Low Search. Low Search also allows you to establish collections where your search term is evaluated in various Custom Fields according to weights that you specify. (So you can design whether the "green" color or the "Green" category would come up first in the results, if it matters.) Nov 27, 2012 at 16:28
2

As Isaac states this is not possible with the search tag. I know you were giving an example of the syntax you would like but the search doesn't work in this way.

I think you may need to approach the build in a different way. It seems like you would like to filter results based on certain fields. EE doesn't have any real solution for this as standard. It has been achieved before by using either SuperSearch or Low Search. John Henry gave a great talk at EEUK using SuperSearch in this way so it's possible but complex.

I have built a product store before and used Low Seg2Cat to great effect. Create category groups for each of options you'd like to filter the entries by. Then use Seg2Cat to map the category_ids from the url to the entries category parameter.

You can do this using the magic {segment_category_ids}. It also won't matter what order your segments are in but setting out a standard structure would be a good idea. Also you'll need to restrict your category options to unique names.

Eg.

Set up 2 category groups:

  • colour:

    • red
    • green
    • blue
  • collection:

    • collection a
    • collection b

URL: example.com/products/red/collection_a/

In the index template of 'products' template group:

{exp:channel:entries 
  channel="products"
  dynamic="no" 
  category="{segment_category_ids}"
}

{segment_category_ids} will contain a pipe delimited string of the category_ids from the url segments, like: 2|45|4|9

In this way you can filter your entries based on the url.

1

One of the option you can try is to write a simple plugin extending the channel module. That sounds harder than it is. Here is a nice article by DMLogic to get you started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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