SHOW:
|
|
- or go back to the newest paste.
| 1 | if (! function_exists('sort_query_posts_by'))
| |
| 2 | {
| |
| 3 | function sort_query_posts_by($order_by, $order = 'asc', $locale = '') | |
| 4 | {
| |
| 5 | global $wp_query; | |
| 6 | $order_by = strtolower($order_by); | |
| 7 | $order = strtolower($order); | |
| 8 | ||
| 9 | if ($order_by == 'rand') {
| |
| 10 | shuffle($wp_query->posts); | |
| 11 | return; | |
| 12 | } | |
| 13 | ||
| 14 | if ($order_by == 'none') {
| |
| 15 | $order_by = 'id'; | |
| 16 | $order = 'asc'; | |
| 17 | } | |
| 18 | ||
| 19 | $props = array( | |
| 20 | 'author' => 'return sqp_compare_by_number($o1->post_author, $o2->post_author, '.$order.');', | |
| 21 | 'comment_count' => 'return sqp_compare_by_number($o1->comment_count, $o2->comment_count, '.$order.');', | |
| 22 | 'date' => 'return sqp_compare_by_number(strtotime($o1->post_date), strtotime($o2->post_date), '.$order.');', | |
| 23 | 'id' => 'return sqp_compare_by_number($o1->ID, $o2->ID, '.$order.');', | |
| 24 | 'menu_order' => 'return sqp_compare_by_number($o1->menu_order, $o2->menu_order, '.$order.');', | |
| 25 | 'modified' => 'return sqp_compare_by_number(strtotime($o1->post_modified), strtotime($o2->post_modified), '.$order.');', | |
| 26 | 'parent' => 'return sqp_compare_by_number($o1->post_parent, $o2->post_parent, '.$order.');', | |
| 27 | 'title' => 'return sqp_compare_by_string($o1->post_title, $o2->post_title, '.$order.', '.$locale.');' | |
| 28 | ); | |
| 29 | ||
| 30 | usort($wp_query->posts, create_function('$o1, $o2', $props[$order_by]));
| |
| 31 | } | |
| 32 | ||
| 33 | function sqp_compare_by_number($n1, $n2, $order) | |
| 34 | {
| |
| 35 | $n1 = (int) $n1; | |
| 36 | $n2 = (int) $n2; | |
| 37 | $v = $n1 > $n2 ? 1 : ($n1 < $n2 ? -1 : 0); | |
| 38 | return ($order == 'desc') ? $v * -1 : $v; | |
| 39 | } | |
| 40 | ||
| 41 | function sqp_compare_by_string($s1, $s2, $order, $locale) | |
| 42 | {
| |
| 43 | if (!empty($locale)) {
| |
| 44 | - | setlocale(LC_COLLATE, $locale); |
| 44 | + | setlocale(LC_ALL, $locale); |
| 45 | - | $v = strcoll($s1, $s2); |
| 45 | + | $charset = get_option('blog_charset');
|
| 46 | - | } else {
|
| 46 | + | $s1 = iconv($charset, 'ASCII//TRANSLIT', $s1); |
| 47 | - | $v = strnatcasecmp($s1, $s2); |
| 47 | + | $s2 = iconv($charset, 'ASCII//TRANSLIT', $s1); |
| 48 | } | |
| 49 | ||
| 50 | $v = strnatcasecmp($s1, $s2); | |
| 51 | ||
| 52 | return ($order == 'desc') ? $v * -1 : $v; | |
| 53 | - | |
| 53 | + | |
| 54 | } |