0

Our admin dashboard list of our custom post type shows by default two columns - title and categories.

We want to improve the list, and to add the foobar custom field. So we do that as follows:

add_action('manage_myproduct_posts_columns', 'manage_myproduct_posts_columns');
function manage_heatherproduct_posts_columns($post_columns) {
  $_REQUEST['mode'] = 'excerpt';
    $post_columns = array(
        'cb' => $post_columns['cb'],
        'title' =>  'Product',        
        'categories' => 'Categories',
        'myproduct_foobar' => 'foobar',
        );
    return $post_columns;
}

That shows the title and the foobar field fine (code which pulls in the foobar data not shown). But now, the categories column is semi-broken. One of the categories is shown, correctly, for those products which fit into that category. But none of the other categories ever show up. Instead we just see a dash.

What are we doing wrong?

NB. I've not included the code where we pull in the data for the custom column foobar as the category problem seems to occur even if we comment that out.

1 Answer 1

0

It turns out we had inadvertently created a single conventional category together with our dozen or so custom categories (this probably happened using the WP Ultimate CSV Importer which, for some of our imports, I think we'd filled in the category field hoping it would map to our custom category; it didn't and we forgot about it).

We checked the wp_term_taxonomy table in the database. It clearly distinguishes the different types of category you're operating, so this helped us to figure it out.

Having established the cause of the problem, the solution is easy. Just having 'categories' => 'Categories', and hoping it will pick up your custom categories is never going to work.

We needed to explicitly pull in the categories, just like any other custom field on a custom post type. Like so, using get_the_term_list:

add_action('manage_posts_custom_column', 'manage_myproduct_custom_column',10,2);
function manage_myproduct_custom_column($column_key,$post_id) {
    global $pagenow;
    global $wpdb;
    $post = get_post($post_id);

    if ($post->post_type=='myproduct' && is_admin() && $pagenow=='edit.php')  {
        //echo ( get_post_meta($post_id,$column_key,true) ) ? get_post_meta($post_id,$column_key,true) : "";        
        switch ( $column_key) {
            case "myproduct_categories":
                  echo get_the_term_list($post->ID,'myproduct_categories','',', ','');
                  break;        
            case "myproduct_foobar":
                  if(get_post_meta( $post_id, 'myproduct_foobar', true)=='Available') {echo 'Y';};
                  break;

        }
    }
}

Your Answer

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

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