Skip to content

Instantly share code, notes, and snippets.

@scribu
Last active June 5, 2024 20:30
Show Gist options
  • Save scribu/856587 to your computer and use it in GitHub Desktop.
Save scribu/856587 to your computer and use it in GitHub Desktop.
'product' post type + 'color' taxonomy
<?php
// Register the post type and taxonomy
function init_product_cpt() {
register_post_type( 'product', array(
'public' => true,
'label' => __( 'Products', 'my-plugin' )
) );
register_taxonomy( 'color', 'product', array(
'label' => __( 'Color', 'my-plugin' )
) );
}
add_action( 'init', 'init_product_cpt' );
// Register the column
function color_column_register( $columns ) {
$columns['color'] = __( 'Color', 'my-plugin' );
return $columns;
}
add_filter( 'manage_edit-product_columns', 'color_column_register' );
// Register the column as sortable
function color_column_register_sortable( $columns ) {
$columns['color'] = 'color';
return $columns;
}
add_filter( 'manage_edit-product_sortable_columns', 'color_column_register_sortable' );
// Display the column content
function color_column_display( $column_name, $post_id ) {
if ( 'color' != $column_name )
return;
the_terms( $post_id, 'color' );
}
add_action( 'manage_product_posts_custom_column', 'color_column_display', 10, 2 );
@thomasfromwood
Copy link

Hello, thanks for the code! Just wanted to point out that the hook on the last line manage_posts_custom_column must be manage_product_posts_custom_column to work.

@scribu
Copy link
Author

scribu commented Jun 5, 2024

Thanks, updated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment