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 have a custom post type which uses dates and i have my custom columns set up in the admin. I need to be able to do order by that column by default - i have tried this code which I found on the net but after playing with it for an hour i still couldn't make it work and it only seemed to want to sort by title; can anyone help me.

https://www.facebook.com/note.php?note_id=147785468608658

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I had to do this on a program management system. this should do the trick.

add_filter( 'pre_get_posts', 'my_sort_posts_listing' );
function my_sort_posts_listing( $wp_query ) {
    if ( is_admin() && $wp_query->get( 'post_type' ) == 'my_post_type' ) {
        $wp_query->set( 'meta_key', '_my_date_meta' );
        $wp_query->set( 'orderby', 'meta_value' );
        $wp_query->set( 'order', 'DESC' );
    }

    return $wp_query;
}

assuming your dates are stored like YYY-MM-DD

share|improve this answer
    
Worked perfectly thanks –  byronyasgur Jan 12 '12 at 22:44

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.