Last active
July 22, 2017 18:25
-
-
Save javierarcheni/4b6065d47ceec9441773 to your computer and use it in GitHub Desktop.
Ordenar entradas de un objeto Post de WordPress por índice numérico o de texto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Devuelve las entradas ordenadas de un objeto Post de WordPress | |
* Útil para ordenar Custom Post Types o Post Objects en Advanced Custom Fields | |
* Ejemplo objectRSort($post_objects, 'post_title'); */ | |
function objectRSort(&$object, $key) | |
{ | |
for ($i = count($object) - 1; $i >= 0; $i--) | |
{ | |
$swapped = false; | |
for ($j = 0; $j < $i; $j++) | |
{ | |
if(is_numeric($object[$j]->$key)) | |
{ | |
if ($object[$j]->$key > $object[$j + 1]->$key) | |
{ | |
$tmp = $object[$j]; | |
$object[$j] = $object[$j + 1]; | |
$object[$j + 1] = $tmp; | |
$swapped = true; | |
} | |
} | |
else | |
{ | |
if (strtolower($object[$j]->$key) > strtolower($object[$j + 1]->$key)) | |
{ | |
$tmp = $object[$j]; | |
$object[$j] = $object[$j + 1]; | |
$object[$j + 1] = $tmp; | |
$swapped = true; | |
} | |
} | |
} | |
if (!$swapped) return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment