Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to sort the posts (in the edit screen (wp-admin/edit.php)) by meta value corresponding to an event date but I can't get it to work properly...

For example, ASC returns:

2012/10/12
2012/12/11
2012/09/7

and DESC returns:

2012/09/7
2012/12/11
2012/10/12

I'm using the following functions to create the sortable custom columns:

function event_date_column_display( $name ) {
    global $post;
    switch ($name) {
        case 'event-date':
            $pid = $post->ID;
            $event_date = get_post_meta($pid, '_event_date', TRUE);

            if ($event_date) {
                echo $event_date;
            }
    }
}

function event_date_column_register_sortable( $columns ) {
    $columns['event-date'] = 'event-date';  
    return $columns;
}

Any hint on how to fix this?

share|improve this question

1 Answer 1

Is it not an option to insert the sorting directly to the WP_Query class? It has all the parameters for handling this kind of situation http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

If you want to do that in the code, you can first validate all the dates, then convert them to the standard ISO(yyyy-mm-dd) format & then use the standard php functions to sort them like sort, ksort, (usort combined with strcmp) or any other depending on your data

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.