11

I need to pull out all the entries in a channel that are of a certain type of category. But, i also want to be able to then sort the entries by the value of a custom field in that channel.

So right now, i have something like:

{exp:channel:entries channel="my_channel" limit="9" category="{segment_3_category_id}"}
     <li>{title}</li>
{/exp:channel:entries}

And in that channel, i have a custom field of "price". I want to amend that channel tag to order by my custom field called "price".

How can i achieve this?

  • To clarify for those reading the question, {segment_3_category_id} in the example code is a product of the Low Seg2Cat add-on. – Derek Hogue Nov 28 '12 at 13:45
17
{exp:channel:entries channel="my_channel" orderby="price" sort="asc"}

Ordering entries by custom fields comes by default with ExpressionEngine: http://expressionengine.com/user_guide/modules/channel/channel_entries.html#orderby

  • 2
    Also, from the docs... The orderby= and sort= parameters can accept multiple values using the pipe character. This allows you to have multiple levels of ordering and then specify the sort rules for those levels, e.g. orderby="screen_name|date" sort="asc|desc" – Dave O'Brien Nov 22 '12 at 18:19
  • I have an issue.I have a points table and there are two teams locked at the same point.The one with more goals needs to be on top..any idea how that can be achieved? – user2133 Jun 1 '14 at 10:38
  • you can pass multiple values to the "ordeby" parameter. So in your case this should do the trick: orderby="points|goals” – janvl Jun 2 '14 at 12:40
5

One addendum to the other answers (though they are perfectly correct) that may be relevant to you given your choice of sorting field:

It's worth noting that if you want your price field ordered data to be ordered numerically in order of increasing/decreasing scale, you will need to change the field content type to decimal (assuming decimal prices). To do this go into your field settings, and you'll find "Field Content" under the "Field Options" section.

Otherwise these values:

 2,11,3

will end up sorted alphabetically like so:

11,2,3

when what you want is:

2,3,11
1

This is very easy to do Just order by your custom field - I do believe you are limited to fieldtype of "text" other fieldtypes won't work with the orderby parameter.

orderby="name_of_field"

See the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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