Displaying column for custom taxonomy on custom post type admin page
Asked Answered
S

2

5

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.

Solent answered 21/3, 2012 at 3:11 Comment(0)
D
7

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?

Distributary answered 21/3, 2012 at 16:14 Comment(8)
mj7: Thanks so much. This is great. The taxonomies are now displayed. One question, though: How would I make the slugs and taxonomies display as links?Solent
Obviously it depends what you want to link them to - the below would link each Taxonomy in the list to the edit page for that taxonomy: $types = $wpdb->get_results("SELECT name,term_id 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 '<a href="/wp-admin/edit-tags.php?action=edit&taxonomy=TAXONOMY-TYPE-NAME&tag_ID='.$type->term_id.'&post_type=CUSTTYPE">'.$type->name.'</a>, '; } break;Distributary
I can't honestly say what the correct location to the page you need is - the best way to find out is to copy the link of the target page - paste it into the a tag around the type and then insert the $type->term_id variable. Note - you need to make sure you add the term_id field to the SQL SELECT statement as in the example aboveDistributary
Corrected the link error (my bad). Links are working! Now, if you haven't entirely lost patience with the topic, how to make the link display the entries for that taxonomy (similar to selecting the category on the main Posts admin page), rather than return the edit screen for the taxonomy?Solent
Try changing the link to: echo '<a href="/wp-admin/edit.php?post_type=CUSTTYPE&TAXONOMY-TYPE-NAME='.$type->term_id.'">'.$type->name.'</a>, '; Obviously changing the CUSTTYPE and TAXONOMY-TYPE-NAME to your correct names.Distributary
That solution created a link in this format: /wp-admin/edit.php?post_type=CUSTTYPE&TAXONOMY-TYPE-NAME=6. Taxonomy IDs are correct. So, I'm not sure why, but that link returned an empty list.Solent
You may need to use the slug instead of the ID - add "slug" to the SELECT statement - i.e.: SELECT name,term_id,slug FROM $wpdb->posts - and then set the link to: '<a href="/wp-admin/edit.php?post_type=CUSTTYPE&TAXONOMY-TYPE-NAME='.$type->slug.'">'.$type->name.'</a>, '; Failing that take a look at how the URL is formed when you manually select a category from the drop down filter box.Distributary
That did it! I thank you for sticking with the problem and can imagine others will be looking for this solution. You're a WordPress scholar!Solent
D
25

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

'show_ui'           => true,
'show_admin_column' => true,
Durmast answered 30/4, 2013 at 13:21 Comment(0)
D
7

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?

Distributary answered 21/3, 2012 at 16:14 Comment(8)
mj7: Thanks so much. This is great. The taxonomies are now displayed. One question, though: How would I make the slugs and taxonomies display as links?Solent
Obviously it depends what you want to link them to - the below would link each Taxonomy in the list to the edit page for that taxonomy: $types = $wpdb->get_results("SELECT name,term_id 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 '<a href="/wp-admin/edit-tags.php?action=edit&taxonomy=TAXONOMY-TYPE-NAME&tag_ID='.$type->term_id.'&post_type=CUSTTYPE">'.$type->name.'</a>, '; } break;Distributary
I can't honestly say what the correct location to the page you need is - the best way to find out is to copy the link of the target page - paste it into the a tag around the type and then insert the $type->term_id variable. Note - you need to make sure you add the term_id field to the SQL SELECT statement as in the example aboveDistributary
Corrected the link error (my bad). Links are working! Now, if you haven't entirely lost patience with the topic, how to make the link display the entries for that taxonomy (similar to selecting the category on the main Posts admin page), rather than return the edit screen for the taxonomy?Solent
Try changing the link to: echo '<a href="/wp-admin/edit.php?post_type=CUSTTYPE&TAXONOMY-TYPE-NAME='.$type->term_id.'">'.$type->name.'</a>, '; Obviously changing the CUSTTYPE and TAXONOMY-TYPE-NAME to your correct names.Distributary
That solution created a link in this format: /wp-admin/edit.php?post_type=CUSTTYPE&TAXONOMY-TYPE-NAME=6. Taxonomy IDs are correct. So, I'm not sure why, but that link returned an empty list.Solent
You may need to use the slug instead of the ID - add "slug" to the SELECT statement - i.e.: SELECT name,term_id,slug FROM $wpdb->posts - and then set the link to: '<a href="/wp-admin/edit.php?post_type=CUSTTYPE&TAXONOMY-TYPE-NAME='.$type->slug.'">'.$type->name.'</a>, '; Failing that take a look at how the URL is formed when you manually select a category from the drop down filter box.Distributary
That did it! I thank you for sticking with the problem and can imagine others will be looking for this solution. You're a WordPress scholar!Solent

© 2022 - 2024 — McMap. All rights reserved.