| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_MGL_OrderBy { |
| 4 |
|
| 5 |
public $admin = null; |
| 6 |
|
| 7 |
static function run( $images, $orderby = null, $order = 'asc' ) { |
| 8 |
$sqlOrderBy = ''; |
| 9 |
|
| 10 |
// Check params |
| 11 |
if ( $orderby === 'ids' ) { |
| 12 |
$images = $order === 'asc' ? sort( $images ) : rsort( $images ); |
| 13 |
} |
| 14 |
else if ( $orderby === 'title' ) { |
| 15 |
$sqlOrderBy = $order === 'asc' ? ' ORDER BY p.post_title ASC' : ' ORDER BY p.post_title DESC'; |
| 16 |
} |
| 17 |
else if ( $orderby === 'date' ) { |
| 18 |
$sqlOrderBy = $order === 'asc' ? ' ORDER BY p.post_date ASC' : ' ORDER BY p.post_date DESC'; |
| 19 |
} |
| 20 |
|
| 21 |
// Apply sort |
| 22 |
if ( !empty( $sqlOrderBy ) ) { |
| 23 |
global $wpdb; |
| 24 |
$wpIdsPlaceHolders = array_fill( 0, count( $images ), '%d' ); |
| 25 |
$wpIdsPlaceHolders = implode( ', ', $wpIdsPlaceHolders ); |
| 26 |
$query = $wpdb->prepare( "SELECT p.ID |
| 27 |
FROM $wpdb->posts p |
| 28 |
WHERE p.ID IN ($wpIdsPlaceHolders)" . $sqlOrderBy, $images ); |
| 29 |
$images = $wpdb->get_col( $query ); |
| 30 |
|
| 31 |
} |
| 32 |
return $images; |
| 33 |
} |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
?> |
| 38 |
|