Support » Plugins » Hacks » Sorting Category by most recently updated

  • Hi,

    I’m new to the community, but I’ve been using WordPress (user level) for a long time now. I’m working now on a custom template and I can’t figure out how to get something done the way I need it.

    I’m trying to make a home template where posts are listed by category, I.E:

    Post|Post|Post|Post (Load more)

    Category 2
    Post|Post|Post|Post (Load More)

    …and so on. I got it working sorting the categories with get_categories and then looping through them and through the posts: ($categories=get_categories() -> foreach $categeories as $category -> $posts=get_posts() -> foreach $posts as $post…) But I need to make the template show the most recently updated category on top, then the second one, etc. and since WP can only sort categories by id, name, slug, count or term group I’m having the hardest time trying to figure out how to do this, as my PHP knowledge is quite limited (I’m not dev of any sorts)

    I’m beginning to think this might not be possible, can you guys give me some hints?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    Within the limits of php and available data, most things are possible if you’re willing to do the coding.

    There’s no dates stored with categories, only with posts. One approach would be to start storing dates for each category every time a post is added to it. That will not help until posts are added to all categories. But then you could easily sort categories by date.

    Or you could find the latest post for each category and sort by those dates. This would work well with out the need to store more data, but to do so efficiently would require significant re-working of the template.

    The easiest and most inefficient would be to loop through each category to get the latest post date of each, saving it into an array with the category id. Sort the array by date then step through each category id in the array and do the query and display just as you do now.

    The more efficient method would be to query all the posts needed for each category to begin with, caching the results until the proper category order can be determined, then outputting the cached data accordingly. By using cached data though, you lose the ability to use WP_Query properties and methods, so you would need to roll your own.

Viewing 1 replies (of 1 total)
  • The topic ‘Sorting Category by most recently updated’ is closed to new replies.