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.

Orderby Category

January 11, 2011 7:00pm

Subscribe [3]
  • #1 / Jan 11, 2011 7:00pm

    CDT

    93 posts

    I’m assuming (since it isn’t working) that you can’t orderby=category_id?  More specifically, is it possible to orderby a custom category field?

    Ideas for a workaround?


    (Also, this is the only forum I seem to have access to add a new post.  All other forums are grayed out.  Hope it is the right place to ask this.)

    [Mod Edit: Moved to the Community Help forum]

  • #2 / Jan 12, 2011 8:39am

    Sue Crocker

    26054 posts

    Hi, CDT. You didn’t mention which version/build of EE you’re using. You have licenses for both EE1.x and EE2.x.

    However if you’re referring to an exp:channel:entries loop, the only things you can order by are here:

    http://ellislab.com/expressionengine/user-guide/modules/channel/parameters.html#par_orderby

    What’s the reasoning behind needing to order by a custom category field?

  • #3 / Jan 12, 2011 9:17am

    CDT

    93 posts

    I didn’t mention the version because the site is currently on 1.x, but will be upgrading soon to 2.x, so the version was irrelevant.  If this was something that could be done in 2.x, they could wait to do it after the upgrade.  If it was 1.x, I was sure it would carry into 2.x.

    The reason for wanting to order by a custom category field is because the category IDs are not in sequence, and the category names are not the alpha sequence desired either.  The custom category field is simply a number “category_sort_order,” to designate way the categories should be sorted.

    I can’t believe, in all this time I’ve used EE, that I never had a reason to sort by category before.  I was just surprised that it couldn’t be done, and thought I must have missed something.  That’s so strange! 

    The category, in this case, is used to define the product’s category (ie, classification).  Not the actual example, but it is similar to something like: Clothing, Electronics, Groceries, etc.  I can’t help but think that other people have wanted to display something like parts or orders lists, but have it sort according to the category, without breaks for category heading (which could be done using a category archive tag, but even then, you don’t have the ability to use all the fields in the weblog/channel).

    I understand why it would be difficult for a exp:weblog/channel tag to be used to sort by category, but since I never had to do it before, was surprised it didn’t exist in some other tag/feature.

    I would think this was about the most straightforward use of the Category function you could have.  The only solution I can come up with then is to (essentially) duplicate the category as a drop-down weblog/channel field, put a number in front of the category name, so it sorts properly, such as 10-Category Name, 20-Category Name, etc. (leaving gaps for future insertions), and then go back through and add the value to every weblog/channel entry.  Duplicate category as a custom field.  Ugh.

    <voice =“9 year old girl”>But I don’t wanna doooo that.</voice>

    (You wouldn’t have in your back-pocket a handy little utility program that fetches matching channel_data, based on category, and allows you to update a channel data field would ya, Sue?)

    Thanks, Sue.

  • #4 / Jan 12, 2011 5:23pm

    Lisa Wess

    20502 posts

    Hi, CDT -

    Your front-end output should follow the ordering you set in the Categories page in the Admin.  You can select “custom” and use the arrows to move the Categories up/down into the order you want.

    Does that help?

  • #5 / Jan 12, 2011 7:03pm

    CDT

    93 posts

    It would, if I could sort weblog/channel entries by category, Lisa.  Unfortunately, as Sue confirmed, you can’t.

  • #6 / Jan 12, 2011 7:04pm

    CDT

    93 posts

    I’ve solved this by adding a drop-down custom field to the weblog/channel that is a sort #/category name, and I can sort by that.  You can close this thread.

    Thanks for you help.

    BTW, is it correct that this is the only Forum to which I’m allowed to create a new post?

  • #7 / Jan 13, 2011 8:17am

    Sue Crocker

    26054 posts

    Glad you have a workaround.

    You should be able to post to lots of different forums, since you have a license for 1.x and 2.x. Have you tried posting to the CodeShare Corner?

  • #8 / Nov 12, 2011 8:39am

    ianryde

    112 posts

    Hi CDT,

    Can i ask you in more detail what you did to solve this,

    I am wanting to sortby specific categories too.

    I have a list of products and at the moment i am sorting them by inventory, but also what to sort them by type:

    For example:

    Size 9 shoes first
    Size 10 shoes after that
    etc. etc.

    But also showing the greatest product inventory items at the top of those groups.

    Any help would be greatly appreciated.

    Thanks,
    Ian

  • #9 / Nov 14, 2011 11:30am

    CDT

    93 posts

    You can create a template that sorts by any custom field. If the attributes, such as shoe size, are custom fields (which I assume they are) you should be able to use a pipe to sort them in the weblog call, first sorting by your inventory custom field, such as:

    orderby="inventory|my_cat|my_cat_two|size" sort="asc|asc|asc|asc"

    If you need a workaround for category sorting, create a custom field that duplicates the category field, and then assign each item to it. If you need to simulate nested categories, you can either create two or more custom fields, or when you name your categories, use a naming convention that would alphabetize the items in a nested fashion, such as
    Clothing, then
    Clothing: Womens, then
    Clothing: Womens: Shoes, etc.

    The above doesn’t give you as much flexibility as having multiple custom fields, but either method will work. It just depends on how how deep your hierarchy is and how likely you are to want to change the category structure. You could decide to go with a combination of both. Any renaming/redo of a structure is going to be a pain. What you want, is a way that enables you to have to change as few records as possible when something like that happens.

    Possibly related: I would think that you would display the sizes themselves 9, 10, 11, etc., separately from the word “Size.” That way you could sort them numerically and then add the word “Size” (as text) to appear before (or after). If you are including the word “Size” in how you are storing the information, such as “Size 10,” then it is sorting alphanumeric, so “Size 9” is going to appear before “Size 10,” but if you separate the word “Size” from “9”, you won’t have that problem. I may have misunderstood what you said, but that might be one way of solving some of your problems.

    Addendum: In re-reading what you wrote, I see better what you’re wanting to do. I think you’ll have to create a drop down value “Size 9 Shoes” (for example) and then assign entries to it. It should work the same as categories, and more so, because you’ll be able to get all the weblog statistics for it (allowing you to provide a count to your customers of how many size 9 shoes there are, for example). If you’re planning on providing a bar on the left size, for example, you could do a weblog call that would display a count of all the stats for each size, as people were clicking various selections. (Assuming that’s what you’re after.) I don’t think you would ever be able to do that with categories, but you would be able to do that by using custom fields.

    Hope that helps.

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

ExpressionEngine News!

#eecms, #events, #releases