Ticket #247 (closed defect (bug): fixed)

Opened 7 years ago

Last modified 6 years ago

Sort get_archive() output by more than just post_date

Reported by: skippy Owned by: skippy
Priority: normal Milestone: 2.1
Component: Template Version: 1.6
Severity: trivial Keywords: bg|squashed bg|commit
Cc:

Description

I have a need to sort the list of archives produced by wp_get_archives() and get_archives() alphabetically by post title, rather than chronologically by post_date. Adding two extra variables: "order" and "sort" to both functions would allow users to define additional sort and order terms. For example:

wp_get_archives("type=postbypost&sort=post_title&order=ASC")

Attachments

template-functions-general.patch Download (2.7 KB) - added by skippy 7 years ago.
add two new parameters to get_archives(): orderby and sort
247.patch Download (979 bytes) - added by skippy 6 years ago.

Change History

Here's an addition to get_archives() that adds support for "type=alpha" to produce an alphabetical list of archive links:

starts around line 363 in wp-includes/template-functions-general.php:

} elseif ('alpha' == $type) {

$arcresults = $wpdb->get_results("SELECT ID, post_date, post_title FROM$wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_title ASC" . $limit); if ($arcresults) {

foreach ($arcresults as $arcresult) {

if ($arcresult->post_date != '0000-00-00 00:00:00') {

$url = get_permalink($arcresult->ID); $arc_title = $arcresult->post_title; if ($arc_title) {

$text = strip_tags($arc_title);

} else {

$text = $arcresult->ID;

} echo get_archives_link($url, $text, $format, $before, $after);

}

}

}

}

here's another, possibly more elegant solution:

line 347 in wp-includes/template-functions-general.php:

} elseif (('postbypost' == $type)
('alpha' == $type)) {

('alpha' == $type) ? $orderby = "post_title ASC " : $orderby = "post_dat

e DESC ";

$arcresults = $wpdb->get_results("SELECT ID, post_date, post_title FROM

$wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY " . $orderby . $limit);

  • Version changed from 1.2 to 1.6
  • Component changed from General to Template
  • Milestone set to 1.6

Here's a better patch for get_archives() and wp_get_archives() that introduces two new parameters:

orderby - defaults to post_date

sort - defaults to DESC

Existing templates should not break as these parameters are added to the end of get_archives().

skippy7 years ago

add two new parameters to get_archives(): orderby and sort

  • Keywords bg|has-patch added
  • Keywords bg|dev-feedback bg|2nd-opinion added
  • Owner changed from anonymous to skippy
  • Status changed from new to assigned

See this cry for help in the forums:  http://wordpress.org/support/topic/41183

Attempts to use examples in Codex on "alphabetizing posts" were foiled by this "bug" and only resolved by the work-round outlined by Skippy.

  • Milestone changed from 1.6 to 1.5.2

comment:9   matt7 years ago

  • Milestone changed from 1.5.2 to 1.6

skippy6 years ago

  • Keywords bg|squashed added; bg|dev-feedback bg|2nd-opinion removed

247.patch adds type=alpha to sort archives alphabetically. This works in wp_get_archives() and get_archives()

  • Keywords bg|commit added; bg|has-patch removed

Helping clean up for skippy

  • Milestone changed from 1.6 to 2.1
  • Status changed from assigned to closed
  • Resolution set to fixed

(In [3549]) Add alpha sort to wp_get_archives(). Props skippy. fixes #247

Note: See TracTickets for help on using tickets.