Support » Fixing WordPress » Admin list custom column sorting

  • Hey,

    I’m using custom post types to store event details, including the date. The date is stored in the custom post type meta as a timestamp. I’ve managed to add a column to the admin list that displays the timestamp. I’ve also managed to make the column sortable, however it seems to be sorting them in an odd order. I guessing that’s because the timestamp is being stored as text rather than an integer.

    For example when sorting in descending order, the 1st February 2011 is appearing above 10th February 2011.

    Any ideas on how `I can get this to sort properly? Thanks in advance.

    Below is my code….

    add_filter('manage_edit-eventdate_columns', 'mv_eventdate_cols');
    add_action('manage_posts_custom_column',  'mv_eventdate_showcols');
    add_filter("manage_edit-eventdate_sortable_columns", 'mv_eventdate_sort');
    
    // Sort admin cols
    function mv_eventdate_sort($columns) {
    	$custom = array(
    		'eventdate'    => 'eventdate_start',
    		'event'    => 'eventdate_event'
    		);
    	return wp_parse_args($custom, $columns);
    }
    
    // Add remove admin columns
    function mv_eventdate_cols($columns) {
    	$columns['eventdate'] = 'Event date & time';
        $columns['event'] = 'Event name';
        unset($columns['date']);
        unset($columns['title']);
        return $columns;
    }
    
    // Show column content
    function mv_eventdate_showcols($name) {
    	global $post;
        switch ($name) {
            case 'event':
            	$eventid = get_post_meta($post->ID, 'eventdate_event', true);
            	$theevent = get_post($eventid);
                echo '<strong><a href="/wp-admin/post.php?post='.$post->ID.'&action=edit" title="Edit this event date">';
                echo $theevent->post_title;
                echo '</a>';
                echo ' - </strong>';
                echo '<a href="/wp-admin/post.php?post='.$theevent->ID.'&action=edit" title="Edit this event">';
                echo '(edit this event)';
                echo '</a>';
                break;
           	case 'eventdate' :
           		$stamp1 = get_post_meta($post->ID, 'eventdate_start', true);
           		$stamp2 = get_post_meta($post->ID, 'eventdate_end', true);
           		$allday = get_post_meta($post->ID, 'eventdate_all_day', true);
                echo '<span style="display:none;">'.$stamp1.'</span><strong><a href="/wp-admin/post.php?post='.$post->ID.'&action=edit" title="Edit this event date">';
    			echo date('jS F Y', $stamp1);
    			echo ' - ';
    			if($allday=="yes") { echo 'All Day'; }
    			else { echo date('H:i', $stamp1)." to ".date('H:i', $stamp2); }
                echo '</a></strong>';
                break;
        }
    }
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Admin list custom column sorting’ is closed to new replies.