0

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?

1

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:

$meta_query = array( 
    'relation' => 'OR',
    array(
      'key' => 'property_reference',
      'value' => false,
      'type' => 'BOOLEAN',
    ),
    array( 
      'key' => 'property_reference',
      'compare' => 'NOT EXISTS',
      'value' => '', //have to set value due to bug: core.trac.wordpress.org/ticket/23268
    ),
    array(
      'key' => 'property_reference',
    )

  );

  $query->set( 'meta_query', $meta_query );  
  $query->set( 'meta_key', 'property_reference' );      
  $query->set( 'orderby', 'meta_value' );

...where property_reference is my custom field.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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