I am trying to use a custom field sort_order to order entries by setting the Field Content type of a Text Input to Integer.

I'm using EE2.

Expected behavior:

  1. Entry 1 with a sort_order of 10
  2. Entry 2 with a sort_order of 20
  3. Entry 3 with a sort_order of 110

Actual behavior:

  1. Entry 1 with a sort_order of 10
  2. Entry 3 with a sort_order of 110
  3. Entry 2 with a sort_order of 20

Channel Field:

  • Field Type: Text Input
  • Field Label: Sort Order
  • Short Name: sort_order
  • Field Options > Field Content: Integer

Template:

{exp:channel:entries channel="work" orderby="sort_order" sort="asc"}

Update

I tried creating a plugin and setting orderby like this: orderby="{exp:parse_int data={sort_order}}". Won't entry parameters always be converted to strings since they are wrapped in "?

Update

Decided to just ensure that my number strings are two digits long so they are in the correct alphabetical order e.g. 01 02 03... 99.

  • Although hacky feeling, the old go-to for EE is to use a Date field for sorting, and simple set your order by going oldest->newest or vice versa, and then sort on that. – jrothafer Aug 7 at 23:50
  • Thanks @jrothafer, but I need to be able to sort by arbitrary numbers that I can update, not by date. – bantuist Aug 8 at 12:14
  • A date field is completely arbitrary. It has nothing to do with today's date, or any date. You set the date you want. You can arbitrarily set the dates you want for your entries in the order you want them to sort by. It just a different way to create an artificial order, and EE can sort by date out of the box. So if you want the sort order to be entry #50, entry #9, entry #880, you give entry #50 the oldest (or newest) date, and entry #9 a date in between the other two. – jrothafer Aug 8 at 13:11
  • Gotcha @jrothafer! That makes sense. Thanks for the explanation. I will try that if nothing else works. – bantuist Aug 8 at 14:33

I think the Custom field sort_order may have value in string format. That's why it is not working.You need to convert it into integer format then try with it.

You can convert it by making small pi.plugin_name.php and wrirting code to convert string to integer. Then place your {exp:plugin_name} code in orderby parameter of Channel entries loop.

I hope above solution will work for you.

  • I created the plugin, but not sure how it would be used to ensure orderby gets an integer orderby="{exp:parse_int data={sort_order}}" – bantuist Aug 8 at 14:38
  • It should returns integer value. Or you can also try (int) with php enable version. – Harsh Barach Aug 8 at 14:53

I cannot replicate your results.

Created a new channel "test" with a single field "counter" set to Text with field entry set to "integer".

Built template with this code:

<h1>Sorting Test</h1>
<h2>Sort with orderby="entry_id" sort="asc"</h2>
<ul>
{exp:channel:entries channel="test" orderby="entry_id" sort="asc"}
    <li>{title} - {counter}</li>
{/exp:channel:entries}
</ul>
<h2>Sort with orderby="counter" sort="asc"</h2>
<ul>
{exp:channel:entries channel="test" orderby="counter" sort="asc"}
    <li>{title} - {counter}</li>
{/exp:channel:entries}
</ul>

This gives the following output:

Sorting Test

Sort with orderby="entry_id" sort="asc"

  • entry 1 - 10
  • entry 2 - 200
  • entry 3 - 110
  • entry 4 - 20
  • entry 5 - 230
  • entry 6 - 220
  • entry 7 - 120

Sort with orderby="counter" sort="asc"

  • entry 1 - 10
  • entry 4 - 20
  • entry 3 - 110
  • entry 7 - 120
  • entry 2 - 200
  • entry 6 - 220
  • entry 5 - 230

Works (i.e. tested) with EE2 and EE4 ... :)

  • EE4 would be nice! I'm stuck with EE2 for now though. Any ideas on how to do it there? – bantuist Aug 8 at 14:25
  • Just ran the identical template with identical data on an EE2 test server and got identical results. So looks like whatever is going on with your system is not specifically EE related. I'd look at two things: first check that the data in your desired search field really is just numbers - if you have junk spaces in it maybe is causing EE to treat the content as strings rather than integers; second, I'm using a late version of EE (2.10.1) so maybe if you have an earlier version there is something about that version that causes problem - if so update to newer version if you can. – jcogs.eecms Aug 8 at 22:06
  • I just noticed that you are using orderby="entry_id". I am trying to order the entries with orderby="counter". – bantuist Aug 9 at 12:06
  • Look again... the template generates two lists. The first is sorted by entry_id, the second by "counter". In the first listing the numbers are not in a sensible order. In the second they are. The code demonstrates that changing the orderby parameter from "entry_id" to "counter" actually works... – jcogs.eecms Aug 9 at 21:13
  • Ok, I see that now! – bantuist Aug 10 at 23:02

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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