1

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.

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!!!

1 Answer 1

2

As of WP 3.5, register_taxonomy accepts a show_admin_column parameter: "Whether to allow automatic creation of taxonomy columns on associated post-types".

2
  • And to really answer your Q: get_the_terms returns an array. Therefore you should run a foreach and use $term->name to get the name, and get_term_link for the URL.
    – diggy
    Jan 16, 2013 at 19:15
  • Wow that was easy huh! I dont suppose it's as easy to make the new columns sortable as they were originally? Jan 16, 2013 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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