php - Metabox select arrays is display post id instead of post name in the custom column
I am having a little trouble displaying a post name in a admin column on my custom page in wordpress. I have everything working but when I call the selected item that was chosen from a drop down from a custom metabox it displays the post ID in the admin column instead of the post name.
Here is the code I am using ( to display the columns)
add_filter('manage_edit-projects_columns', 'edit_projects_columns');
function edit_projects_columns($columns) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Project Title' ),
'client' => __( 'Client' ),
'protype' => __( 'Project Type' ),
'featureImage' => __( 'Project Image' ),
);
return $columns;
}
To Display the selected fields
add_action('manage_projects_posts_custom_column', 'manage_projects_columns', 10, 2);
function manage_projects_columns($column, $post_id, $selected) {
global $post;
switch($column) {
/* Display Column */
case 'client' :
/* Get the post meta. */
$selected = get_post_meta($post->ID, 'a_clients', true);
if (empty($selected))
echo __('');
else
printf(__( '%s' ), $selected);
break;
/* End Display Column */
/* If displaying the 'genre' column. */
case 'protype' :
$terms = get_the_terms($post_id, 'tagwork');
if (!empty($terms)) {
$out = array();
foreach($terms as $term) {
$out[] = sprintf('<a href="%s">%s</a>',
esc_url(add_query_arg(array('post_type' => $post->post_type, 'tagwork' => $term->slug), 'edit.php')),
esc_html(sanitize_term_field('tagwork', $term->name, $term->term_id, 'tagwork', 'tagwork'))
);
}
echo join(', ', $out);
} else {
_e('');
}
break;
case 'featureImage' :
$column = get_the_post_thumbnail($post->ID, 'featureImage');
if (empty($contact))
echo __('');
else
printf(__('%s'), $contact);
break;
default :
break;
}
}
The selected field I am having trouble with is client
/* Display Column */
case 'client' :
/* Get the post meta. */
$selected = get_post_meta($post->ID, 'a_clients', true);
if (empty($selected))
echo __('');
else
printf(__('%s'), $selected);
break;
It is displaying the post ID instead of the post name.How would I go about fixing this?
Extra Note: The column is pulling the client information from a drop down menu on a projects page. The menu is pulling the titles from any new client I add to the client page. This way I can assign a project to a specific client.
Answer
Solution:
In your example
$selected
is being assigned the value of thea_clients
meta value, which seems to be apost_id
. You need to use thispost_id
to fetch theWP_Post
for the client and then use thepost_title
property to print out the proper title. You can leave out theelse
since it's just an empty string.