Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Sort in Post Type Home Screen WordPress

  • SOLVED

I want to sort in my post type home. How is this done?

Thanks.

Answers (1)

2010-07-19

Oleg Butuzov answers:

function _mks($sql){
if (strpos($sql, 'players') !== false){
list($select,$where) = explode("WHERE", $sql);
$orderby = isset($_GET['orderby']) ? $_GET['orderby'] : 'first_name';
$order = isset($_GET['order']) ? $_GET['order'] : 'ASC';

$join = " LEFT JOIN {$GLOBALS['wpdb']->postmeta} as pm ON ( pm.post_id = {$GLOBALS['wpdb']->posts}.ID AND pm.meta_key = '{$orderby}') ";
list($orderby_before, $orderby_after) = explode('ORDER BY', $where);
$sql = $select . $join .' WHERE '. $orderby_before .' GROUP BY ID ORDER BY '.str_replace($GLOBALS['wpdb']->posts.'.post_date DESC', 'pm.meta_value '.$order, $orderby_after);
}

return $sql;
}

if (is_admin()){
add_action('parse_query', 'qqq');
function qqq($q){
if (!empty($q->query_vars['order']) && !empty($q->query_vars['orderby']) && !empty($q->query_vars['post_type']) && $q->query_vars['post_type'] == 'players'){

global $wpdb;
//add_filter('query', 'meta_key_sorter');
add_filter('query', '_mks');
$w->query_vars['order'] = 'asc';
$w->query_vars['orderby'] = $wpdb->posts.'.post_date';

}
return $q;
}
}