Wordpress- Custom columns for custom post types

Step 1. Adding custom columns-

The first step in creating custom columns, is to make these columns available on the edit posts page. WordPress has a variable filter hook for this called manage_edit-{$post_type}_columns for all post types. Since the name of our post type is movie, the name of the hook you’d use is manage_edit-movie_columns.

What we’ll do here is overwrite all of the default columns and set it up however we want. In this example, we’re using five columns:

1. cb: The checkbox column.

2. title: The post title column, which we’re changing to read “Movie.”

3. duration: The column for the duration of the movie (custom metadata).

4. family: The column for the movie family (custom taxonomy).

5. date: The default post date column.

Filtering these columns is fairly simple. You only need to return an array of key/value pairs. The key is the column name and the value is the output of the column header/label.

 
add_filter( 'manage_edit-movie_columns', 'my_edit_movie_columns' ) ;

function my_edit_movie_columns( $columns ) {

    $columns = array(
        'cb' => '',
        'title' => __( 'Movie' ),
        'duration' => __( 'Duration' ),
        'family' => __( 'Family' ),
        'date' => __( 'Date' )
    );

    return $columns;
}

Step 2.Adding content to custom columns

The action hook in this case is manage_{$post_type}_posts_custom_column. Remember, the name of the post type is movie, so this hook becomes manage_movie_posts_custom_column.

 
add_action( 'manage_movie_posts_custom_column', 'my_manage_movie_columns', 10, 2 );

function my_manage_movie_columns( $column, $post_id ) {
    global $post;

    switch( $column ) {

        /* If displaying the 'duration' column. */
        case 'duration' :

            /* Get the post meta. */
            $duration = get_post_meta( $post_id, 'duration', true );

            /* If no duration is found, output a default message. */
            if ( empty( $duration ) )
                echo __( 'Unknown' );

            /* If there is a duration, append 'minutes' to the text string. */
            else
                printf( __( '%s minutes' ), $duration );

            break;

        /* If displaying the 'family' column. */
        case 'family' :

            /* Get the familys for the post. */
            $terms = get_the_terms( $post_id, 'family' );

            /* If terms were found. */
            if ( !empty( $terms ) ) {

                $out = array();

                /* Loop through each term, linking to the 'edit posts' page for the specific term. */
                foreach ( $terms as $term ) {
                    $out[] = sprintf( '%s',
                        esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'family' => $term->slug ), 'edit.php' ) ),
                        esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'family', 'display' ) )
                    );
                }

                /* Join the terms, separating them with a comma. */
                echo join( ', ', $out );
            }

            /* If no terms were found, output a default message. */
            else {
                _e( 'No familys' );
            }

            break;

        /* Just break out of the switch statement for everything else. */
        default :
            break;
    }
}

Step 3. (If you want to ) Making custom columns sortable

The first step is making the duration column sortable by filtering the manage_edit-{$post_type}_sortable_columns hook as shown in the following code.

 
add_filter( 'manage_edit-movie_sortable_columns', 'my_movie_sortable_columns' );

function my_movie_sortable_columns( $columns ) {

    $columns['duration'] = 'duration';

    return $columns;
}



/* Only run our customization on the 'edit.php' page in the admin. */
add_action( 'load-edit.php', 'my_edit_movie_load' );

function my_edit_movie_load() {
    add_filter( 'request', 'my_sort_movies' );
}

/* Sorts the movies. */
function my_sort_movies( $vars ) {

    /* Check if we're viewing the 'movie' post type. */
    if ( isset( $vars['post_type'] ) && 'movie' == $vars['post_type'] ) {

        /* Check if 'orderby' is set to 'duration'. */
        if ( isset( $vars['orderby'] ) && 'duration' == $vars['orderby'] ) {

            /* Merge the query vars with our custom variables. */
            $vars = array_merge(
                $vars,
                array(
                    'meta_key' => 'duration',
                    'orderby' => 'meta_value_num'
                )
            );
        }
    }

    return $vars;
}

Enjoy :)

No comments:

Post a Comment