Make WordPress Core

Opened 20 years ago

Closed 18 years ago

#247 closed defect (bug) (fixed)

Sort get_archive() output by more than just post_date

Reported by: skippy's profile skippy Owned by: skippy's profile skippy
Milestone: 2.1 Priority: normal
Severity: trivial Version: 1.6
Component: Template Keywords: bg|squashed bg|commit
Focuses: 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 (2)

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

Download all attachments as: .zip

Change History (15)

#2 @skippy
19 years ago

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

}

}

}

}

#3 @skippy
19 years ago

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

#4 @skippy
19 years ago

  • Component changed from General to Template
  • Milestone set to 1.6
  • Version changed from 1.2 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().

@skippy
19 years ago

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

#5 @skippy
19 years ago

  • Keywords bg|has-patch added

#6 @skippy
19 years ago

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

#7 @cloudhopper
19 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.

#8 @skippy
19 years ago

  • Milestone changed from 1.6 to 1.5.2

#9 @matt
19 years ago

  • Milestone changed from 1.5.2 to 1.6

@skippy
18 years ago

#10 @skippy
18 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()

#11 @masquerade
18 years ago

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

Helping clean up for skippy

#12 @matt
18 years ago

  • Milestone changed from 1.6 to 2.1

#13 @ryan
18 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

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

Note: See TracTickets for help on using tickets.