Custom columns for multiple custom post types

I currently have 2 issues with Custom Post Types…

First of all I’m trying to setup custom columns in Admin to display and act correctly.

Read More

I have a Custom Post Type called ‘acme_products’ and a Taxonomy called ‘product_type’.

I have followed the Yoast tutorial http://yoast.com/custom-post-type-snippets/ (amongst others!) which has almost got me there…

The problem is under the column ‘Product type’ instead of displaying the ‘product_type’ Taxonomy it simply says ‘Array’, and links to localhost:8888/wp-admin/Array.

I don’t actually mind if it doesn’t link anywhere, so long as the Taxonomy displays correctly.

Code is below plus an explanation of what I changed:

// Add columns to the overview page for a Custom Post Type

function change_columns( $cols ) {
    $cols = array(
        'cb'       => '<input type="checkbox" />',
        'title' => __( 'Product', 'trans' ),
        'product_type' => __( 'Product Type', 'trans' ),
        'date'     => __( 'Date', 'trans' ),
    );
    return $cols;
}
add_filter( "manage_acme_products_posts_columns", "change_columns" );


// Give these new columns some content

// Below I changed 'get_post_meta' to 'get_the_terms'
// This was taken from a further tutorial to target taxonomies rather than post_meta

function custom_columns( $column, $post_id ) {
    switch ( $column ) {
        case "product_type":
            $product_type = get_the_terms( $post_id, 'product_type', true);
            echo '<a href="' . $product_type . '">' . $product_type. '</a>';
            break;
    }
}
add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );


// Make these new columns sortable

function sortable_columns() {
    return array(
        'title' => __( 'title' ),
        'product_type' => __( 'product_type' ),
        'date'     => __( 'date' ),
    );
}
add_filter( "manage_edit-acme_products_sortable_columns", "sortable_columns" );

Further to this problem, I have a second Custom Post Type called ‘acme_courses’ with a Taxonomy of ‘course_locations’.

How would I go about applying the custom columns to a second (and potentially 3rd & 4th) Custom Post Type?

If I duplicate the code above, and change bits to reflect the new Custom Post Type and Taxonomy, it breaks everything! I get a white screen both in admin and on the site.

Thanks in advance!!!

Related posts

Leave a Reply

1 comment