ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Search Custom Field when the field name itself is a variable

July 09, 2012 4:56pm

Subscribe [1]
  • #1 / Jul 09, 2012 4:56pm

    J. Hull

    132 posts

    Is there any way one can search a custom field name, if the field name itself is a variable?

    I’m dealing with two channels: MasterTable and Sections

    MasterTable has 20+ custom fields with DropDown formatting.
    Sections has a Matrix field with 2+ rows per entry and a custom field called CFShort (both text)


    In a perfect world, I need my page to:

    - randomly select one of the Section entries (returning CFShort)
    - randomly select one of the rows from the Matrix field within this Section entry (returning Option)
    - randomly select three entries from MasterTable with a custom field short_name that matches CFShort and has a result that matches Option

    Now I know this has to be possible, and I get close by using search:field_name BUT only by hardcoding the field_name in there…I need it to be dynamically selected from the previous channel query. I can easily grab the first two items above—it’s the third that causes me grief.

    Ultimately I would like this to work:

    {exp:channel:entries channel="MasterTable" orderby="random" limit="3" search:{CFShort}="{Option}"}

    But the whole parse order prevents it from working…

    I’m close, but I just need to work out the logic…any takers?

  • #2 / Jul 11, 2012 11:01am

    Dan Decker

    7338 posts

    Hi J. Hull,

    You can probable do this with an embed and some embed variables:

    {embed='embeds/master-table' my_cf-short='{CFShort}' my_option='{Option}'}
    
    Then… your master-table template
    {exp:channel:entries channel="MasterTable" orderby="random" limit="3" search:{embed:my_cf-short}="{embed:my_option}"}

    What do you think?

    Cheers,

  • #3 / Jul 11, 2012 1:20pm

    J. Hull

    132 posts

    Yes, that would work great! Though I was trying to add another embed to my template.

    I’ve been trying the whole DRY/Stash technique and already had an embed in there.

    Your solution works great, but I also managed to get it to work with some Stash goodness. The link for anyone who is interested is here: Search Custom Field with Stash

    Thanks!

  • #4 / Jul 12, 2012 2:29pm

    Dan Decker

    7338 posts

    Hi J. Hull,

    Excellent! Thanks for the resource!

    If you need anything else, just let us know.

    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases