WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#18832 closed enhancement (fixed)

wp_get_archives should allow for Ascending and Descending ordering

Reported by: tar.gz Owned by: nacin
Milestone: 3.5 Priority: low
Severity: normal Version: 3.3
Component: General Keywords: has-patch commit
Focuses: 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)

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

Download all attachments as: .zip

Change History (14)

comment:1 @scribu3 years ago

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

comment:2 @tar.gz3 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.

@tar.gz3 years ago

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

comment:3 @tar.gz3 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.

comment:4 @scribu3 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';

comment:5 @tar.gz3 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.

comment:6 @scribu3 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

@tar.gz3 years ago

improved version as per comment 5+6

comment:7 @tar.gz3 years ago

New patch submitted.

comment:8 @scribu3 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:

http://wpdevel.wordpress.com/version-3-3-project-schedule/

comment:9 @tar.gz3 years ago

Yep, I'm aware of the feature freeze.

Looking forward to seeing this commited in a future release.

comment:10 @tar.gz3 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?

comment:11 @scribu3 years ago

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

comment:12 @nacin3 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.