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.

Ordering by custom field, empty values problem

November 10, 2012 3:12pm

Subscribe [1]
  • #1 / Nov 10, 2012 3:12pm

    I’m using the channel entries tag to output a list of items in a channel, and I’m ordering by setting the orderby-attribute to the short name of a custom field which may or may not contain a number. The problem is that empty values are last in the ordering, when I want them to be first. So what I want is:

    empty | empty | 2 | 4 | 100

    While what I get is:

    2 | 4 | 100 | empty | empty

    Setting the order from asc to desc would reverse the whole thing obviously, but I only want the items with empty values to be shown first while keeping the list in ascending order. Any way to get this done?

  • #2 / Nov 12, 2012 12:44pm

    Kevin Smith

    4784 posts

    Hi Geert,

    It’s not possible to get the Channel Entries tag to do what you want here, but we might be able to get the Query module to do it. Could you show me the whole Channel Entries tag so that I can see the parameters and output you’re working with? I’ll see what I can come up with as an alternative.

  • #3 / Nov 12, 2012 2:17pm

    Hey Kevin,

    Thanks for your reply. Here’s the code:

    <nav>
      <ul>
        <li class="home{if !segment_1} active{/if}"><a href="/">Home</a></li>
        <li{if segment_1 == "campers"} class="active"{/if}><a href="/campers">Campers</a></li>
        {exp:channel:entries channel="pages" disable="categories|member_data|pagination" search:show_in_nav="Ja" search:nav_title="not IS_EMPTY" orderby="nav_order" search:page_uri="not IS_EMPTY" order="asc" dynamic="no"}
        <li{if segment_1 == url_title} class="active"{/if}><a href="http://{page_uri}">{nav_title}</a></li>
        {/exp:channel:entries}
      </ul>
    </nav>

    As you can see, this code serves up the main navigation. I’m using the pages module and a few custom fields (checkbox show_in_nav, text nav_title, text nav_order). It’s working pretty well, but I want the nav_order field to be optional, so that the client isn’t forced to input a number for every page. Which is why I want the empty fields to show up first and any numbered fields after that.

    I’m also not sure if this is even a decent way to handle navigation, so any advice is appreciated. I think the Structure extension might be great for pages and navigation, but I’m trying to see how far I can go without adding another payed extension to the site.

  • #4 / Nov 13, 2012 5:11pm

    Kevin Smith

    4784 posts

    Hmm… well if that’s the case, what sort of fieldtype are you using for the order? If you use a regular Text Input field and set the Field Content to Number, by default the value will be 0. If the user removes the value on the publish page and submits it blank, it’s 0. That would be the quickest way to solve this, and it’s actually the best way to set it up, too.

  • #5 / Nov 14, 2012 2:14pm

    Aha! Thanks Kevin, changing the field content to number works nicely. Also noticed I had a parameter called order=“asc” in my tag, which should have been sort=“asc”. Those two changes fixed my problem.

    Thanks again, topic may be closed now 😊

  • #6 / Nov 16, 2012 3:07pm

    Dan Decker

    7338 posts

    Thank you for the followup!

    If we can do anything else in the future, please let me know!

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases