Search code examples
wordpresscustom-post-typetaxonomy

Wordpress taxonomies when listing custom post types


I'm registering a custom post type "rv-events" which has a taxonomy "rv-categories". On the admin page where you normally get a table of those posts, you get a table with the title, categories taxonomy and date published as expected.

This is all default and expected, however, now I'm changing the column labels of the table using add_filter('manage_edit-events_columns', 'change_column_titles'). With the 'change_column_titles' function I manage to edit the column titles, but the data for the custom taxonomies column gets lost. I just get a — for the taxonomies instead of it listing my taxonomies as it would when not changing the column names.

For the taxonomy, I have show_admin_column set to true, so this isn't the problem. Anyone else got any idea? Thanks!

So to keep it short the issue is: I cannot show/list the taxonomies in the overview table of a custom post type when I have changed its column labels.

Registering post type:

    $labels = array(
        ...
    );

    $args = array(
        'label' => 'Events',
        'labels' => $labels,
        'public'             => true,
        // 'publicly_queryable' => false,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'events' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'custom-fields', 'thumbnail')
    );

    register_post_type('rv-events', $args);

Registering taxonomy:

    $labels = array( 
        ...
    );

    $args = array( 
        'labels' => $labels,
        'public' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'show_tagcloud' => true,
        'show_admin_column' => true,
        'hierarchical' => true,
        'rewrite' => true,
        'query_var' => true
    );

    register_taxonomy( 'rv_categories', array('rv-events'), $args );

Adding the custom columns

add_filter( 'manage_edit-rv-events_columns', 'column_titles') ;

function column_titles($columns) {
    return array(
        'cb' => '<input type="checkbox" />',
        'title' => 'Title',
        'start_date' => '<i class="fa fa-calendar-o"></i> Start Date',
        'end_date' => '<i class="fa fa-calendar-o"></i> End Date',
        'repeat' => '<i class="fa fa-repeat"></i> Repeat',
        'venue' => '<i class="fa fa-map-marker"></i> Venue',
        'categories' => 'Categories',
        'date' => '<i class="fa fa-calendar-o"></i> Date'
    );
}

Solution

  • Change your titles function to use the actual name of the column you are changing:

    function column_titles($columns) {
        return array(
            'cb' => '<input type="checkbox" />',
            'title' => 'Title',
            'start_date' => '<i class="fa fa-calendar-o"></i> Start Date',
            'end_date' => '<i class="fa fa-calendar-o"></i> End Date',
            'repeat' => '<i class="fa fa-repeat"></i> Repeat',
            'venue' => '<i class="fa fa-map-marker"></i> Venue',
            'taxonomy-rv_categories' => 'Categories',
            'date' => '<i class="fa fa-calendar-o"></i> Date'
        );
    }
    

    'Categories' is reserved for the actual WordPress post categories. If you create a custom taxonomy that is not considered a 'category'.