Wordpress - sort posts by title (numeric)
<?php
//When title has the following format: '1.1. Title'. Edit preg_match expressions as needed
function sort_titles_numeric($a,$b) {
$akey = $a->post_title;
if (preg_match('/^(\d+)\.(\d+)\. /',$akey,$matches)) {
$akey = sprintf('%010d ',$matches[0]) . $akey;
}
$bkey = $b->post_title;
if (preg_match('/^(\d+)\.(\d+)\. /',$bkey,$matches)) {
$bkey = sprintf('%010d ',$matches[0]) . $bkey;
}
if ($akey == $bkey) {
return 0;
}
return ($akey < $bkey) ? -1 : 1;
}
//uasort($posts,'sort_titles_numeric');
?>
//Sort title by article numbers - 'Article 23. ...'
function sort_titles_article_numbers($a,$b) {
$akey = $a->post_title;
if (preg_match('/^Article (\d+)\. /', $akey, $matches)) {
$akey = $matches[1];
}
$bkey = $b->post_title;
if (preg_match('/^Article (\d+)\. /', $bkey, $matches)) {
$bkey = $matches[1];
}
if ($akey == $bkey) {
return 0;
}
return ($akey < $bkey) ? -1 : 1;
}
//uasort($posts,'sort_titles_article_numbers');