1

I have a custom post type called events. In the backend/administration I can list all these custom post types, that is, events:

enter image description here

As you can see, there are three columns in this overview: Title, Tags, and Date. Each of these events has a custom field named eventDate.

My question now is, how can I add a sortable eventDate column to the events overview (pictured above)?

4

The hooks to create custom columns and their associated data for a custom post type are manage_{$post_type}_posts_columns and manage_{$post_type}_posts_custom_column respectively, where {$post_type} is the name of the custom post type.

This example from the documentation removes the author column and adds a taxonomy and meta data column:

// Add the custom columns to the book post type:
    add_filter( 'manage_book_posts_columns', 'set_custom_edit_book_columns' );
    function set_custom_edit_book_columns($columns) {
        unset( $columns['author'] );
        $columns['book_author'] = __( 'Author', 'your_text_domain' );
        $columns['publisher'] = __( 'Publisher', 'your_text_domain' );

        return $columns;
    }

    // Add the data to the custom columns for the book post type:
    add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
    function custom_book_column( $column, $post_id ) {
        switch ( $column ) {

            case 'book_author' :
                $terms = get_the_term_list( $post_id , 'book_author' , '' , ',' , '' );
                if ( is_string( $terms ) )
                    echo $terms;
                else
                    _e( 'Unable to get author(s)', 'your_text_domain' );
                break;

            case 'publisher' :
                echo get_post_meta( $post_id , 'publisher' , true ); 
                break;

        }
    }

Copied from existing ans.

  • is the column sortable? – beta Oct 10 '17 at 11:35
  • no these colums won't be sortable..you have to make it sortable.. – Pravin Work Oct 11 '17 at 7:47
3

Okay, I found an answer myself. To help people who will read this in future, this is what I did:

1) This explains how to add a column: http://www.deluxeblogtips.com/add-custom-column/

2) This explains how to add a sortable column: https://wordpress.org/support/topic/admin-column-sorting/

3

Here is the entire code for this:

add_filter('manage_edit-video_columns', 'my_columns');
function my_columns($columns) {
    $columns['eventDate'] = 'Event Date';
    return $columns;
}

add_action('manage_posts_custom_column',  'my_show_columns');
function my_show_columns($name) {
    global $post;
    switch ($name) {
        case 'eventDate':
            $eventDate = get_post_meta($post->ID, 'eventDate', true);
            echo $eventDate;
    }
}

add_filter( 'manage_edit-video_sortable_columns', 'my_sortable_date_column' );
function my_sortable_date_column( $columns ) {
    $columns['eventDate'] = 'Event Date';

    return $columns;
}

Thanks

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.