Sort order issue in expressionengine 1.x

You might noticed the issue for sorting stuff in expressionengine cms to show entries if you’ve worked with this, that is if you’ve a custom field which stores number value and later you want sort the entries by that field – the entries sorted in wrong order! If you didn’t noticed that yet, explaining…

Let you have a custom field named “display_order”. This field stores integer value. Later you’ve tag like bellow to show entries in specific display order by this field –


{exp:weblog:entries  channel="channel_name"  orderby="display_order" sort="asc"  limit="15"}
content
{/exp:weblog:entries}

Above code pulls data in wrong order. Because Expressionengine creates varchar type field in table for every custom field. So if an entry has value 2 for display_order and 10, the entry with value 10 will comes first as mysql query treats them as string.

For this, you can do a quick trick. First find which field is created for the custom field you created, as per above example find out the field created for custom field “display_order”. Lets say table field for this custom field is field_id_20. Alter this field as integer type. So prepare a query like below and execute –

ALTER TABLE exp_weblog_data CHANGE `field_id_20` `field_id_20` INT(11) DEFAULT NULL

So, now see – issue is fixed 😉

Advertisements

One thought on “Sort order issue in expressionengine 1.x

  1. This solution probably worked fine in EE 1.x. However, this is still an issue in EE 2.9.2 and I wouldn’t recommend using the above method to fix it there as it will break publishing of entries in the CP (the reason being EE will submit an empty text ” instead of the mysql default value for the field and thus crash with a PHP/MySQL error when saving an entry).

    An alternative fix that works fine is to use this Number fieldtype: http://devot-ee.com/add-ons/number

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s