Opened 20 months ago
Closed 9 months ago
#18832 closed enhancement (fixed)
wp_get_archives should allow for Ascending and Descending ordering
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | low | Milestone: | 3.5 |
| Component: | General | Version: | 3.3 |
| Severity: | normal | Keywords: | has-patch commit |
| Cc: |
Description
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:
http://wordpress.org/support/topic/sort-posts-in-ascending-order-through-wp_get_archives
http://wordpress.org/support/topic/chronological-sidebar-archives?replies=3
http://wordpress.org/support/topic/monthly-archives-ascendingdescending?replies=15
http://wordpress.org/support/topic/archives-index-ascending-order?replies=4
http://wordpress.org/support/topic/archives-ascending?replies=6
Some people try to solve the problem by editing the core files:
http://www.bestwpthemez.com/wordpress/how-to-set-archives-list-in-ascending-order-1633/
Attachments (2)
Change History (14)
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.
- 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.
- 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';
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.
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
- Keywords commit added
- Milestone changed from Awaiting Review to Future Release
Looking good.
Unfortunately, it's too late to be included in 3.3:
Yep, I'm aware of the feature freeze.
Looking forward to seeing this commited in a future release.
comment:10
tar.gz — 10 months 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?
comment:11
scribu — 10 months ago
- Milestone changed from Future Release to 3.5
- Priority changed from normal to low
comment:12
nacin — 9 months ago
- Owner set to nacin
- Resolution set to fixed
- Status changed from new to closed
In [21610]:

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