The only things I see different in your code are the add_filter()
and add_action()
calls (e.g. the callback/function name), so you can use the same functions for all of the types:
function update_dog_type_columns( $columns ) {
// your code
return $columns;
}
function update_dog_type_column( $column, $post_id ) {
// your code
}
And then you can do something like this to register the filter/action callback for each (post) type:
foreach ( array( 'poodle', 'retriever', 'labrador', 'etc' ) as $type ) {
add_filter( "manage_{$type}_posts_columns", 'update_dog_type_columns' );
add_action( "manage_{$type}_posts_custom_column", 'update_dog_type_column', 10, 2 );
}
Alternate Solution
The above is good, but this might be better to make sure your callback doesn’t run for other types:
You can hook to just manage_posts_columns
and use the $post_type
parameter to conditionally run your filter/action:
function update_dog_type_columns( $columns, $post_type ) {
if ( in_array( $post_type, array( 'poodle', 'retriever', 'labrador', 'etc' ) ) ) {
// your code
}
return $columns;
}
add_filter( 'manage_posts_columns', 'update_dog_type_columns', 10, 2 );
And then hook to manage_pages_custom_column
and/or manage_posts_custom_column
, depending on whether any of the “dog” post types is hierarchical or not:
function update_dog_type_column( $column, $post_id ) {
$post_type = get_current_screen()->post_type;
if ( ! in_array( $post_type, array( 'poodle', 'retriever', 'labrador', 'etc' ) ) ) {
return;
}
switch ( $column ) {
// your code
}
}
add_action( 'manage_pages_custom_column', 'update_dog_type_column', 10, 2 ); // for hierarchical post types
add_action( 'manage_posts_custom_column', 'update_dog_type_column', 10, 2 ); // non-hierarchical post types
But either way, you would only need just two functions.
So I hope that helps? 🙂