1

My entries have a field containing a third-party product id and a price, separated by a hyphen, like so:

p666a-12.99

In the template, a plugin is returning me some product ids, and I want to return all entries containing those product ids. I know I can fetch all entries, reduce the custom field down to the product id and compare it to the product ids returned from the plugin, but that seems a bit tardy.

Can I filter my entries using the custom field directly?

{# pluginIds = 'p666a, p678a' #}
{% set entries = craft.entries.section('sectionName').customField(pluginIds) %}
3
  • What's the reason for the product ID and the price being saved to the same database column? Can you change this?
    – carlcs
    Oct 16, 2015 at 13:21
  • +1 to @carlcs. If at all possible, I'd separate them out into different fields. :)
    – Brad Bell
    Oct 16, 2015 at 15:42
  • The plugin presents the client with a dropdown list of products from a third-party, and I think a dropdown is the best way to do that. But, yes, ideally I'd populate two database columns using that field - any examples for doing that from a dropdown, within a plugin? Oct 20, 2015 at 11:40

1 Answer 1

4

You could use the Preparse plugin to save the product ID part of your field in a new hidden field.

Add a new "productId" Preparse field to your entry type with this code in the field's settings:

{{ entry.customField|split('-')|first }}

To update all existing entries, re-save them by going into Settings → Sections → Section Name and click Save. You can now set up a criteria model exactly like you mentioned in your question, except that you now use the Preparse field as your custom field parameter:

{% set entries = craft.entries.section('sectionName').productId(pluginIds) %}
2
  • This could definitely work but I'd love to populate two columns from the one dropdown instead, inside the plugin I've created. Oct 20, 2015 at 11:40
  • Well.. I think you should better ask a new question for this problem. I'm not sure I understand how many plugins are involved and how they save.
    – carlcs
    Oct 22, 2015 at 7:13

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.