Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#18832 closed enhancement (fixed)

wp_get_archives should allow for Ascending and Descending ordering

Reported by: targz's profile tar.gz Owned by: nacin's profile nacin
Milestone: 3.5 Priority: low
Severity: normal Version: 3.3
Component: General Keywords: has-patch commit
Focuses: Cc:


The wp_get_archives function offers various options for listing content by year, month, week, day, postname.

However, it offers only one sorting direction: from the newest to the oldest. This makes certainly sense in the classical WP use case: a blog. But since WP is now a full-featured CMS, it seems logical that the choice of the sorting order should be left to the user.

It should be possible to define this direction by using order=ASC / order=DESC.

If the option gets introduced, but the default left at the current state (order=DESC), the change would not affect existing sites while improving the overall functionality.

This feature has been requested by several users over the years:

Some people try to solve the problem by editing the core files:

Attachments (2)

18832.diff (2.9 KB) - added by tar.gz 12 years ago.
patch for wp_get_archives in wp-includes/general-template.php
18832.2.diff (3.2 KB) - added by tar.gz 12 years ago.
improved version as per comment 5+6

Download all attachments as: .zip

Change History (14)

#1 @scribu
12 years ago

As with most tickets, it's much more likely to get fixed if there's a patch available.

#2 @tar.gz
12 years ago

Sure... The problem is, I'm a visual frontend designer, with rather poor php skillz.

All I understand is that the function is defined in wp-includes/general-template.php, and that instead of hardcoding DESC here, we would need a variable that can be either ASC or DESC.

I will give it a try and report back.

12 years ago

patch for wp_get_archives in wp-includes/general-template.php

#3 @tar.gz
12 years ago

  • Version set to 3.3

Following Scribu's encouragement I just submitted my first patch :)

Be aware that this edit was complete guesswork on my part. I simply replaced the hardcoded DESC with a $order variable.

I tested this on a blank WP 3.3 install with the twentyeleven theme. wp_get_archives now accepts

'order' => 'ASC'

and it's working for yearly, monthly, weekly, daily archives.

It does not work for postbypost - replacing DESC with $order (at line 1001) results in no output.

I could live with that, since the ASC option makes most sense with the yearly/monthly archives.

As one could expect, the change does not affect the alphabetic sorting with alpha.

#4 @scribu
12 years ago

  • Keywords has-patch added

Ok, one important change you need to make:

Currently, a user could pass 'order' => 'DESC, ID ASC'. While it might be useful, it's not a good idea to allow such input.

You should make sure that 'order' can only be 'asc' or 'desc' (case insensitive):

$order = ( 'DESC' == strtoupper( $order ) ) ? 'DESC' : 'ASC';

#5 @tar.gz
12 years ago

As I said, my knowledge of the php syntax is rudimentary, so I'm not entirely sure what the line of code above is doing.

If I integrate it into the function, it fixes the situation where 'order' would be any value different from 'asc' or 'desc', *but* it spits out the results in ASC order, while it would be more logical to keep it as the default (DESC).

What about the following rule:

if ( 'ASC' != strtoupper( $order ) ) {
		  $order = 'DESC';

This will accept ASC (case insensitive), and anything else simply returns DESC.

#6 @scribu
12 years ago

I could invert the check in my one-liner, but your solution is more readable, assuming you indent it properly in the final patch. :P

12 years ago

improved version as per comment 5+6

#7 @tar.gz
12 years ago

New patch submitted.

#8 @scribu
12 years ago

  • Keywords commit added
  • Milestone changed from Awaiting Review to Future Release

Looking good.

Unfortunately, it's too late to be included in 3.3:

#9 @tar.gz
12 years ago

Yep, I'm aware of the feature freeze.

Looking forward to seeing this commited in a future release.

#10 @tar.gz
12 years ago

Since WP 3.5 is getting prepared... is there anything I should do to request this patch to be moved into the official scope?

#11 @scribu
12 years ago

  • Milestone changed from Future Release to 3.5
  • Priority changed from normal to low

#12 @nacin
12 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In [21610]:

Add 'order' to wp_get_archives(). props tar.gz. fixes #18832.

Note: See TracTickets for help on using tickets.