Skip to content

Instantly share code, notes, and snippets.

@SebastianTroc
Created August 10, 2012 08:57
Show Gist options
  • Select an option

  • Save SebastianTroc/3312711 to your computer and use it in GitHub Desktop.

Select an option

Save SebastianTroc/3312711 to your computer and use it in GitHub Desktop.
change plugin: Sort Query Posts with possibility to set a locale for collation
if (! function_exists('sort_query_posts_by'))
{
function sort_query_posts_by($order_by, $order = 'asc', $locale = '')
{
global $wp_query;
$order_by = strtolower($order_by);
$order = strtolower($order);
if ($order_by == 'rand') {
shuffle($wp_query->posts);
return;
}
if ($order_by == 'none') {
$order_by = 'id';
$order = 'asc';
}
$props = array(
'author' => 'return sqp_compare_by_number($o1->post_author, $o2->post_author, '.$order.');',
'comment_count' => 'return sqp_compare_by_number($o1->comment_count, $o2->comment_count, '.$order.');',
'date' => 'return sqp_compare_by_number(strtotime($o1->post_date), strtotime($o2->post_date), '.$order.');',
'id' => 'return sqp_compare_by_number($o1->ID, $o2->ID, '.$order.');',
'menu_order' => 'return sqp_compare_by_number($o1->menu_order, $o2->menu_order, '.$order.');',
'modified' => 'return sqp_compare_by_number(strtotime($o1->post_modified), strtotime($o2->post_modified), '.$order.');',
'parent' => 'return sqp_compare_by_number($o1->post_parent, $o2->post_parent, '.$order.');',
'title' => 'return sqp_compare_by_string($o1->post_title, $o2->post_title, '.$order.', '.$locale.');'
);
usort($wp_query->posts, create_function('$o1, $o2', $props[$order_by]));
}
function sqp_compare_by_number($n1, $n2, $order)
{
$n1 = (int) $n1;
$n2 = (int) $n2;
$v = $n1 > $n2 ? 1 : ($n1 < $n2 ? -1 : 0);
return ($order == 'desc') ? $v * -1 : $v;
}
function sqp_compare_by_string($s1, $s2, $order, $locale)
{
if (!empty($locale)) {
setlocale(LC_ALL, $locale);
$charset = get_option('blog_charset');
$s1 = iconv($charset, 'ASCII//TRANSLIT', $s1);
$s2 = iconv($charset, 'ASCII//TRANSLIT', $s2);
}
$v = strnatcasecmp($s1, $s2);
return ($order == 'desc') ? $v * -1 : $v;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment