Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying column for custom taxonomy on custom post type admin page

I'd like to add a column to my custom post type admin window to display my custom taxonomy.

This solution provided by @SarthakGupta creates the column for me but echoes neither the taxonomy nor the "No taxonomy set" message: Showing custom taxonomy column in custom posts type listings

I've seen similar solutions posted elsewhere, with similar complaints about the empty taxonomy column.

What could be causing the problem? Is it possible this solution does not work with WordPress 3.3?

Thanks.

like image 571
user705100 Avatar asked Mar 21 '12 03:03

user705100


People also ask

How do I add custom taxonomy to custom post type?

' So make sure you have a custom post type created before you begin creating your taxonomies. Next, go to CPT UI » Add/Edit Taxonomies menu item in the WordPress admin area to create your first taxonomy. On this screen, you will need to do the following: Create your taxonomy slug (this will go in your URL)

How do I display categories of my custom post type?

To get the custom post type categories you need to change the arguments passed into the wp_list_categories function. You need to define the taxonomy argument. If you have a custom post type for your products then to display all the categories for products you need to use the following snippet.

What is taxonomy in custom post type?

Custom Taxonomy for Custom Post Types. Taxonomies are a great way to group things together and help us to search posts belonging to a specific group. In WordPress we generally use Categories and Tags as taxonomies. The steps given below explain how to create custom taxonomies for your CPT.


2 Answers

From Wordpress 3.5 you can enable it when you registering custom taxonomy by adding this:

'show_ui'           => true,
'show_admin_column' => true,
like image 54
Luko Del Ponte Avatar answered Sep 30 '22 18:09

Luko Del Ponte


I had this issue and found that replacing the WP functions get_the_terms with a custom query worked better - try (where custtype is the name of your custom post type):

// custom columns
add_filter("manage_edit-custtype_columns", "custtype_columns");
add_action("manage_posts_custom_column", "custtype_custom_columns",10,2);

function custtype_columns($columns){
    $columns = array(
        "cb" => "<input type=\"checkbox\" />",
        "title" => "Title",
        "slug" => "URL Slug",
        "custtype-type" => "Custom Taxonomy"
    );
    return $columns;
}

function custtype_custom_columns($column,$id) {
    global $wpdb;
        switch ($column) {
        case 'custtype-type':
            $types = $wpdb->get_results("SELECT name FROM $wpdb->posts LEFT OUTER JOIN $wpdb->term_relationships ON ID = object_id LEFT OUTER JOIN $wpdb->terms ON term_taxonomy_id = term_id WHERE ID = {$id}");
            foreach($types as $loopId => $type) {
                echo $type->name.', ';
            }
            break;
        case 'slug':
            $text = basename(get_post_permalink($id));
            echo $text;
            break;
        default:
            break;
        } // end switch
}

Does that work at all?

like image 37
mj7 Avatar answered Sep 30 '22 18:09

mj7