WordPress.org

Ready to get started?Download WordPress

Forums

Admin list custom column sorting (6 posts)

  1. gbuckingham89
    Member
    Posted 3 years ago #

    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;
        }
    }
  2. sales@4bco.com
    Member
    Posted 3 years ago #

    If the date stamp is stored as text, try strtotime function to first convert it to a true time stamp, then sort....I had a similar problem and that function go it to work properly....beware, I did not find that strtodate was a recognized function, at least not in my version of WP

  3. gbuckingham89
    Member
    Posted 3 years ago #

    Thanks, it might be silly question, but where would I need to use the strtotime to function?

    Is the column being sorted by the value in the database, or the content of the HTML column?

  4. sales@4bco.com
    Member
    Posted 3 years ago #

    Before you pass it to your sort function

  5. gbuckingham89
    Member
    Posted 3 years ago #

    Still had no luck with this. If anyone else has a suggestion I would be grateful.

  6. tzeldin88
    Member
    Posted 3 years ago #

    I finally got the values in a custom admin column sorted correctly. See my post here
    http://wordpress.org/support/topic/admin-column-sorting/

Topic Closed

This topic has been closed to new replies.

About this Topic