Skip Navigation

[Resolved] Display Taxonomy in Custom Posts Type View

This support ticket is created 3 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by jacobB-3 3 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#2183135
Screen Shot 2021-09-29 at 10.54.46 AM.png

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

#2183377

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/

#2183853

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;
}
}

#2184311

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

#2185033

Thanks, Luo! That last bit of code worked wonders.