Function to get the name in database table from the comma separated string
Question
I already created admin column in WP using this code:
add_filter('manage_pages_columns', 'page_custom_cols', 10);
function page_custom_cols($pages_columns, $post_type) {
$pages_columns['user_group'] = 'User Group';
return $pages_columns;
}
Now I added the function to get the user group id, this field is already in wp_post table but in a comma separated text just like this: " 1,7,2 " and I was able to display it using this function:
add_action('manage_pages_custom_column', 'display_page_custom_cols', 10, 2);
function display_page_custom_cols($col_name, $post_id) {
if ('user_group' == $col_name) {
$pages = get_post($post_id);
echo $pages->group_access;
}
}
And it was showing now: http://screencast.com/t/z1uJ3KinklN
My problem right now is I want to display the the User Group Name not the ID It was in the table 'custom_user_group': http://screencast.com/t/StBmVBfD0
Please help.
Solution
Not tested but this should work:
add_action('manage_pages_custom_column', 'display_page_custom_cols', 10, 2);
function display_page_custom_cols($col_name, $post_id) {
global $wpdb;
$user_group = $wpdb->get_results("SELECT * From custom_user_group",OBJECT_K);
if ('user_group' == $col_name) {
$pages = explode(',',get_post($post_id));
$output = array();
foreach ($pages as $page ) {
$output[] = $user_group[$page]->GroupName;
}
echo implode(',',$output);
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange