Skip to content

Instantly share code, notes, and snippets.

@agustinprosperi
Last active May 12, 2020 11:51
Show Gist options
  • Save agustinprosperi/2ac185feadf89d5f1302a32ce3ef4036 to your computer and use it in GitHub Desktop.
Save agustinprosperi/2ac185feadf89d5f1302a32ce3ef4036 to your computer and use it in GitHub Desktop.
tt_reorder_multiarray_by_numeric_subvalue
<?php
// tt_reorder_multiarray_by_numeric_subvalue
function tt_reorder_multiarray_by_numeric_subvalue($matriz, $subkey){
function cmp($key) {
return function($a, $b) use ($key){
$an = intval($a[ $key ]);
$bn = intval($b[ $key ]);
if($an==$bn){
return 0;
}
// para ordenar de mayor a menor
return ($an<$bn) ? -1 : 1;
};
}
usort($matriz, cmp($subkey));
return $matriz;
}
// ejemplo de uso
$arr = array(
array('nro'=>'5', 'url'=>'5.com'),
array('nro'=>'8', 'url'=>'8.com'),
array('nro'=>'1', 'url'=>'1.com')
);
var_dump(tt_reorder_multiarray_by_numeric_subvalue($arr, 'nro')); exit();
/**
* Funcion para obtener ano/mes de los posts para options de un select
* @return array Array de objetos con propiedades 'ano, mes, cantidad'
*/
function tt_anos_meses_para_select($post_type = 'post', $post_status = array('publish')) {
global $wpdb;
$post_status = array_map(function ($v) { return "'$v'"; }, $post_status);
$post_status = implode(', ', $post_status);
$query = "SELECT YEAR(post_date) AS 'ano', MONTH(post_date) AS 'mes', COUNT(ID) AS 'cantidad'
FROM {$wpdb->posts}
WHERE post_type LIKE '{$post_type}' AND post_status IN ({$post_status})
GROUP BY YEAR(post_date), MONTH(post_date)
ORDER BY YEAR(post_date) DESC, MONTH(post_date) DESC";
return $wpdb->get_results($query);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment