最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Sort custom meta column by other meta value

programmeradmin3浏览0评论

I have a post type named "event" and I store two meta values for these posts: _start_time and _start_time_unix. The first one contains a string representation of a date and the other one a Unix timestamp of that date.

On edit.php I only want a column for _start_time meta but it has to be sortable by the order of, you guessed it, _start_time_unix. This is because the string meta value can sometimes contain the time and sometimes not, and even then the date format may end up being a kind that's not any good for sorting.

Here's the code I use

add_filter('manage_event_posts_columns', 'zz_events_columns');
function zz_events_columns( $columns ) {
    $columns['event_start']  = 'Starting';
    ...
    return $columns;

}

add_filter( 'manage_edit-event_sortable_columns', 'zz_events_sortable_columns' );
function zz_events_sortable_columns( $columns )    {
    $columns['event_start']  = 'Starting';
    ...
    return $columns;
}

add_action( 'manage_event_posts_custom_column', 'zz_events_columns_render', 10, 2 );
function zz_events_columns_render( $column_name, $post_id ) {
    if( $column_name == 'event_start' ) {
        $event_start = get_post_meta( $post_id, '_event_start', true );
        echo $event_start;
    }
    ...
}

add_action( 'pre_get_posts', 'zz_events_columns_orderby' );
function zz_events_columns_orderby( $query ) {
    global $pagenow;

    if ( ! is_admin() || 'edit.php' != $pagenow || 'event' != $query->get( 'post_type' ) )  {
        return;
    }

    $orderby = $query->get( 'orderby' );

    switch ( $orderby ) {
        case 'event_start':
            $query->set( 'meta_key', '_event_start_unix' );
            $query->set( 'orderby', 'meta_value_num' );
            break;

        ...

        default:
            break;
    }

}

I was thinking that it would work if I just set 'meta_key' to '_event_start_unix' for the 'event_start' case but it still sorts wrong. Not sure if what I'm trying to achieve is even possible.

发布评论

评论列表(0)

  1. 暂无评论