public
Last active

'product' post type + 'color' taxonomy

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?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_posts_custom_column', 'color_column_display', 10, 2 );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.