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

Opened 4 years ago

Last modified 3 years ago

Sort get_archive() output by more than just post_date

Reported by: skippy Assigned to: 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 (2.7 kB) - added by skippy on 08/10/05 01:50:00.
add two new parameters to get_archives(): orderby and sort
247.patch (1.0 kB) - added by skippy on 11/05/05 18:05:53.

Change History

09/01/04 16:26:45 changed by skippy

12/16/04 17:56:04 changed by skippy

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);

}

}

}

}

12/16/04 18:09:29 changed by skippy

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);

08/10/05 01:49:17 changed by skippy

  • 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().

08/10/05 01:50:00 changed by skippy

  • attachment template-functions-general.patch added.

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

08/10/05 01:51:32 changed by skippy

  • keywords set to bg|has-patch.

08/10/05 01:53:58 changed by skippy

  • keywords changed from bg|has-patch to bg|has-patch bg|dev-feedback bg|2nd-opinion.
  • owner changed from anonymous to skippy.
  • status changed from new to assigned.

08/10/05 15:11:31 changed by cloudhopper

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.

08/11/05 12:46:32 changed by skippy

  • milestone changed from 1.6 to 1.5.2.

08/14/05 18:23:03 changed by matt

  • milestone changed from 1.5.2 to 1.6.

11/05/05 18:05:53 changed by skippy

  • attachment 247.patch added.

11/05/05 18:15:29 changed by skippy

  • keywords changed from bg|has-patch bg|dev-feedback bg|2nd-opinion to bg|has-patch bg|squashed.

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

11/05/05 21:33:41 changed by masquerade

  • keywords changed from bg|has-patch bg|squashed to bg|squashed bg|commit.

Helping clean up for skippy

11/17/05 01:29:11 changed by matt

  • milestone changed from 1.6 to 2.1.

02/19/06 02:07:16 changed by ryan

  • status changed from assigned to closed.
  • resolution set to fixed.

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