Search code examples
phpwordpresscustom-post-typetaxonomy

Displayin custom taxonomies in custom post type columns


I've looked over several posts about adding custom taxonomies to CPT columns; I'm able to get everything working except actually displaying said taxonomies (publication). Here's my CPT code:

add_action( 'init', 'pb_custom_post_type' );
function pb_custom_post_type() {
  $labels = array(
  'name'               => _x( 'Press', 'post type general name' ),
  'singular_name'      => _x( 'Press', 'post type singular name' ),
  'add_new'            => _x( 'Add New', 'review' ),
  'add_new_item'       => __( 'Add New Press' ),
  'edit_item'          => __( 'Edit Press' ),
  'new_item'           => __( 'New Press' ),
  'all_items'          => __( 'All Press' ),
  'view_item'          => __( 'View Press' ),
  'search_items'       => __( 'Search Press' ),
  'not_found'          => __( 'No press found' ),
  'not_found_in_trash' => __( 'No press found in the Trash' ), 
  'parent_item_colon'  => '',
  'menu_name'          => 'Press'
);
  $args = array(
  'labels'        => $labels,
  'description'   => 'Press information',
  'public'        => true,
  'menu_position' => 20,
  'supports'      => array( 'title', 'editor', 'thumbnail' ),
  'has_archive'   => true,
);
  register_post_type( 'press', $args ); 
}

add_filter( 'manage_edit-press_columns', 'my_edit_press_columns' ) ;
function my_edit_press_columns( $columns ) {
  $columns = array(
    'cb' => '<input type="checkbox" />',
    'title' => __( 'Title' ),
    'publication' => __( 'Publication' ),
    'date' => __( 'Date' )
  );

  return $columns;
}

Custom Taxonomy

add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
    register_taxonomy( 'publication', 'press', array( 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ) );
}

Now I've seen two different choices, either trying to add show_ui/show_admin_column in the taxonomy arguments, or having another function with switch statements. I've tried both below, is there something crucial I'm missing?

1

add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
  $args = array (   
    'show_ui'           => true,
    'show_admin_column' => true,
  );
   register_taxonomy( 'publication', 'press', array( 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ), $args );
}

2

function custom_columns( $column, $post_id ) {
  switch ( $column ) {
    case "publication":
      echo get_post_meta( $post_id, 'publication', true);
      break;
  }
}
add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );

Solution

  • Remove your custom columns function, and add 'show_admin_column' => true to the actual array of args for register_taxonomy:

    register_taxonomy( 'publication', 'press', array( 'show_admin_column' => true, 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ) );
    

    Edit: you might also want to add 'taxonomies' => array( 'publication' ) to the register_post_type args.