Hello,
I'm trying to tweak the display in WordPress of my custom post type "People" with a new column for custom taxonomy (slug: bsia-people). It seems very similar to the question asked in this support thread nearly a decade ago: https://toolset.com/forums/topic/adding-columns-to-custom-post-list-display/
Based on that thread, here's the code I'm using:
//Add custom column
add_filter('manage_edit-people_columns', 'my_columns_head');
function my_columns_head($defaults) {
$defaults['AAA'] = 'Affiliation';
return $defaults;
}
//Add rows data
add_action( 'manage_people_posts_custom_column' , 'my_custom_column', 10, 2 );
function my_custom_column($column, $post_id ){
switch ( $column ) {
case ' AAA ':
echo get_post_meta( $post_id , 'wpcf-bsia-people' , true );
break;
}
}
As you can see in the screenshot, the column is showing up with the title Affiliation but it isn't being populated. Am I missing something to get this to work?
thanks,
Jacob
Hello,
Since it is a custom taxonomy "bsia-people", it is not custom fields, you need to use other function to get the post terms, for example, replace this line from:
echo get_post_meta( $post_id , 'wpcf-bsia-people' , true );
To:
$term_list = wp_get_post_terms( $post_id, 'bsia-people', array( 'fields' => 'names' ) );
print_r( $term_list );
More help:
https://developer.wordpress.org/reference/functions/wp_get_post_terms/
Thanks, Luo. I tried making that change, but am still not seeing the custom taxonomy terms pop up in WordPress. Here's my code now:
//Add custom column
add_filter('manage_edit-people_columns', 'my_columns_head');
function my_columns_head($defaults) {
$defaults['AAA'] = 'Affiliation';
return $defaults;
}
//Add rows data
add_action( 'manage_people_posts_custom_column' , 'my_custom_column', 10, 2 );
function my_custom_column($column, $post_id ){
switch ( $column ) {
case ' AAA ':
$term_list = wp_get_post_terms( $post_id, 'bsia-people', array( 'fields' => 'names' ) );
print_r( $term_list );
break;
}
}
Please try to modify your custom PHP codes as below:
/* Display custom column stickiness */
function display_posts_taxonomy( $column, $post_id ) {
if ($column == 'bsia-people'){
$term_list = wp_get_post_terms( $post_id, 'bsia-people', array( 'fields' => 'names' ) );
echo implode(',', $term_list);
}
}
add_action( 'manage_people_posts_custom_column' , 'display_posts_taxonomy', 10, 2 );
/* Add custom column to post list */
function add_taxonomy_column( $columns ) {
return array_merge( $columns,
array( 'bsia-people' => __( 'bsia-people', 'your_text_domain' ) ) );
}
add_filter( 'manage_people_posts_columns' , 'add_taxonomy_column' );
And test again
Thanks, Luo! That last bit of code worked wonders.