I have a custom post type of ‘gigs’ and a metabox (using meta-box plugin ) with event start date and time saved like: 2013-11-29 20:30.
If I save a draft with no time/date set then it shows up in the published or draft posts admin column as expected.
However when I try to order the default screen to have future gigs at the top then the gigs without a date yet don’t show up in the list, although there is one added to the counts of ‘all’ and ‘draft’ in the status row selector at the top.
I am using the following ( based on https://wordpress.stackexchange.com/a/66478/7401 )
/* Sort posts in wp_list_table by column in ascending or descending order. */
function custom_post_order($query){
global $prefix;
/*
Set post types.
_builtin => true returns WordPress default post types.
_builtin => false returns custom registered post types.
*/
$post_types = get_post_types(array('_builtin' => false), 'names');
/* The current post type. */
$post_type = $query->get('post_type');
/* Check post types. */
if(in_array($post_type, $post_types) && $post_type == 'gigs'){
/* Post Column: e.g. title */
if($query->get('orderby') == ''){
$query->set('orderby', 'meta_value');
}
if($query->get('meta_key') == ''){
$query->set('meta_key', $prefix . 'startdatetime');
}
}
}
if(is_admin()){
add_action('pre_get_posts', 'custom_post_order');
}
Is there a way to get the event with no date/time included in the default admin list, preferably at the top?
I too was struggling with this (only I was sorting by a text-based meta value). Here’s what seems to have fixed the problem:
…where
property_reference
is my custom field.Most likely the meta key is not set for the posts which don’t have time/date set. The meta_key ‘startdatetime’ query that you are trying to do retrieves only the posts for which that meta key is set, and this is the reason you you don’t get those posts in the result.
I don’t know of any simple sql query that can do what you want. You might need to consult people with more sql expertise for that. Then you can change the actual sql query being done.