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.

Display if custom fields aren't empty.

May 25, 2012 2:48am

Subscribe [4]
  • #1 / May 25, 2012 2:48am

    Jason Varga

    26 posts

    Hi,

    I’d like to display a list of entries depending on whether at least one of three custom fields have been entered.

    {exp:channel:entries
    channel="products"
    category="{category_id}”
      dynamic=“no”
      search:custom_field_1=“not IS_EMPTY”
      search:custom_field_2=“not IS_EMPTY”
      search:custom_field_3=“not IS_EMPTY”}
    {/exp:channel:entries}

    This isn’t working because it’s checking that all 3 fields aren’t empty.
    How can I check that at least 1 isn’t empty.

    (In my example, my exp:channel:entries is wrapped with a exp:channel:categories tag. That’s where the category_id is coming from)

    If the answer is to use the query module, could you please give me an example? I’m not so good with SQL 😊
    Thanks!

  • #2 / May 25, 2012 9:27am

    Rob Allen

    3105 posts

    I’d say that you may need to look to using conditionals inside the channel entries tag instead of using paramaters, eg

    {exp:channel:entries
      channel=“products”
      category=”{category_id}”
      dynamic=“no”}
    
    {if custom_field1 != ""}
    Stuff
    {/if}
    
    {if custom_field2 != ""}
    More stuff
    {/if}
    
    {if custom_field3 != ""}
    Even more stuff
    {/if}
    
    {/exp:channel:entries}
  • #3 / May 25, 2012 9:40am

    Jason Varga

    26 posts

    Thanks, but I need to be able to use {if count == 1} and {if count == total_results}.
    Unfortunately your solution won’t let me do that accurately.

  • #4 / May 25, 2012 9:51am

    Rob Allen

    3105 posts

    You may be able to use advanced conditionals here, by checking if any of the fields has content:

    {if custom_field1 != "" OR custom_field2 != "" OR custom_field3 != ""}
    
        {if count == "1"}
        Start
        {/if}
    
        Output content
    
    
        {if count == total_results}
        End
        {/if}
    
    {/if}
  • #5 / May 25, 2012 11:53am

    Jason Varga

    26 posts

    Even if you use {count} and {total_results} inside a conditional, they will display the number according to the entries tag as a whole…

    If I have 10 entries, and only 2 match the conditional:
    {total_results} will still be 10, not 2.
    {count} won’t necessarily be 1 and 2.

  • #6 / May 31, 2012 9:52pm

    Jason Varga

    26 posts

    Does anyone else have any advice?

  • #7 / Jun 14, 2012 9:07pm

    Alex Kendrick

    203 posts

    This query, when used with the query module (and modified to have custom_field_id that matches your data) should do the trick for you

    SELECT title, field_id_136, field_id_137, field_id_138
    FROM exp_channel_titles
    JOIN exp_channel_data ON exp_channel_data.entry_id = exp_channel_titles.entry_id
    WHERE field_id_136 != '' OR field_id_137 != '' OR field_id_138 != ''
  • #8 / Jun 15, 2012 3:37am

    willm

    30 posts

    Take a look at Mark Croxton’s Search Fields plugin: https://github.com/croxton/Search_fields

    It allows the same search options as the channel:entries tag, but let’s you set an operator of AND/OR for the conditions. From the docs:

    {exp:search_fields 
        search:title="keyword" 
        search:custom_field="keyword" 
        search:cat_name="keyword" 
        operator="OR" 
        channel="my_channel" 
        parse="inward"}
        {exp:channel:entries entry_id="{search_results}" disable="member_data|categories" dynamic="no" orderby="title" sort="asc" limit="10"}
            <a href="http://{page_url}">{title}</a>
        {/exp:channel:entries}
    {/exp:search_fields}
  • #9 / Jun 15, 2012 12:20pm

    Jason Varga

    26 posts

    Thanks for the help everyone, the Search Fields plugin worked perfectly.
    It seems a lot more manageable than using SQL.

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

ExpressionEngine News!

#eecms, #events, #releases