Ticket #247 (closed defect (bug): fixed)
Sort get_archive() output by more than just post_date
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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
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().
-
attachment
template-functions-general.patch
added
add two new parameters to get_archives(): orderby and sort
- Keywords bg|dev-feedback bg|2nd-opinion added
- Owner changed from anonymous to skippy
- Status changed from new to assigned
comment:7
cloudhopper — 7 years ago
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.
comment:10
skippy — 6 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()
comment:11
masquerade — 6 years ago
- Keywords bg|commit added; bg|has-patch removed
Helping clean up for skippy
comment:13
ryan — 6 years ago
- Status changed from assigned to closed
- Resolution set to fixed
