Last active
June 5, 2024 20:30
-
-
Save scribu/856587 to your computer and use it in GitHub Desktop.
'product' post type + 'color' taxonomy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.