Wordpress: Sort posts chronologically by custom field
I have some posts used as events, with a custom field for the event's date. I'm looking for ordering thoses posts by event's date (chronologically). I'm wondering:
- Is it possible to set the event's date custom field as a "date" format (for now, it's a simple type=text).
- Howto order thoses posts if i always use the same format (like dd/mm/yyyy). I need to get the 3 close coming events (not the last 3).
Did wordpress has a built-in function who cou开发者_如何学编程ld compare a date to the today's date?
thanks
I just did this! (www.wherewordsgo.com)
okay, i only got it working for 'pages' but.
but if you make a meta box for 'deadline' and then put the date in yyyy/mm/dd format and add this to your functions.php it might help:
add_action('wp', 'check_page'); function check_page () { if (is_page()) { add_filter('get_previous_post_sort', 'sort_it'); add_filter('get_next_post_sort', 'sort_it'); add_filter('posts_orderby', 'sort_it' ); add_filter('posts_join', 'join_it' ); add_filter('posts_where', 'where_it' );
function sort_it () {
global $wpdb;
return " $wpdb->postmeta.meta_value ASC ";
}
function join_it( $join ) {
global $wpdb;
$join .= " LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
return $join;
}
function where_it( $where ) {
global $wpdb;
$where .= "AND $wpdb->postmeta.meta_key = 'deadline' ";
return $where;
}
}
I got that from some site (which I have lost the link for... big WP dude though I think) and changed it as needed. Like I said it works only for custom pages on my build (I'm using a custom type), but may well just work for everything if you change the 'is_page()' function at the start to whatever is relative for you.
Hope that helps
精彩评论