Support » Plugins and Hacks » Hacks » Metadata – List categories by ID

Metadata – List categories by ID

Viewing 7 replies - 1 through 7 (of 7 total)
  • You have the right idea, maybe just a simple syntax error has you stymied? Do you have WP_DEBUG set to true in wp-config.php?

    Something like wp_list_categories( array('orderby'=> 'ID')); should do it.

    Thank you for your reply. I do have debug set to true, and it has confirmed that when I alter this function incorrectly, it “breaks” the site.

    So it looks like the line of code I am trying to alter is:
    $categories_list = get_the_category_list(__( ', ', 'twentytwelve' ) );

    But if I simply replace with:
    $categories_list = wp_list_categories( array('orderby'=> 'ID'));

    What I get is a ul of all the categories I have created for the site. Below that the $utility_text reads “This entry was posted in and tagged…” and then goes on to list the tags; i.e. it doesn’t show the categories inside the $utility_text.

    I’m sure that, as you suggested, I am missing something simple in terms of syntax is perhaps the way I am typing argument or string but I can’t figure it out.

    I’m afraid I focused on your mention of wp_list_categories() and order by ID, and failed to consider what you’re really trying to do. I thus misled you, my apologies, hopefully you learned something though?

    I don’t think you can get wp_list_categories() to only return categories associated with a post. And you can’t get the get_the_category_list() function to order categories by some arbitrary value. No wonder you’re stuck! It’s not a simple syntax fix. I’m afraid to get what you want, it will be slightly involved. (but would be a nice learning experience?) I think the best approach is to use get_the_category() to get an array of category objects associated with the post, then sort the array yourself with usort(). Since your array is of objects, you can sort by any object value you like, such as ID.

    Finally, you use a foreach loop to build the $categories_list string by concatenating each category name and ‘, ‘. This probably sounds like a bit much, but you’ll probably be surprised in the end by how little code is really required for this. As always, don’t hesitate to come back here if you get stuck.

    bcworkz – Thank you again for your reply and for taking the time to think about my question. I will keep working on it using your suggestions.

    I am sure in the end will seem simple, but I think the funny thing is I will do a lot of work (and learning) to achieve a result that the user probably won’t even notice (listing categories by id rather than by name). But it does seem to me that WordPress defaults to ordering by name rather than id, so I would think if I just figured out how or why it does that I’d be most of the way to solving the problem.

    Well, if you were to review the source code for get_the_category_list(), you could trace where the data comes from. After a chain of several increasingly lower level functions, you will arrive at wp_get_object_terms(), defined in wp-includes/taxonomy.php. You will see one of the defaults defined for the query is orderby' => 'name'. You will also see there are no filters or hooks available to alter this default 🙁

    I must say I find your attitude and willingness to learn rather endearing, which gave me more incentive to help you. If you had simply requested code you could paste into your functions.php file, I may have passed your question over.

    Now I’ve grown curious, what benefit do you see to ordering by ID? It’s not something a user would normally even be aware of. Sorting an array of objects by a property is a worthy coding exercise in itself, but I don’t really see the point. Not that what I think matters, it is after all your site, not mine. But as I said, I’m curious.

    The short answer is: learning how it’s done. This all started because my website is for a magazine, and I decided that for the content from the actual printed magazine, I decided that I did not want to display the metadata related to the author and the date (the author because we generally don’t use bylines, and the data to make the content less “dated”). As I said, most users won’t even realize that there is different metadata appearing, but I wanted to see if I could figure it out.

    I asked many people, none of whom could really help me past the beginning stages, months of playing around myself to see what worked, and I took a 10-hour PHP class on Lynda, at which time it clicked to me that I could just kind of duplicate the twentytwelve meta function with an if and an else in between. Then, when I solved it after all that work, I saw that the categories (and, by extension, the tags) were listing in alphabetical order (I guess that’s sortby => name), when I had actually set them up and ordered them and their respective ids in a particular order (by magazine content, by magazine issue, by magazine department, etc.). So of course I wanted to make things harder for myself.

    So, yes, it’s a learning exercise. I think the level of knowledge needed to solve the problem is above my head (I wouldn’t have known exactly where to look for the functions you described above, though I had a pretty good idea), so it will probably have to wait.

    Thanks again for your help.

    I see, fair enough. Thanks for explaining. I’m always happy to help. Cheers.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Metadata – List categories by ID’ is closed to new replies.