I have a Custom Post Type named conferences
(same slug, supports custom-fields), which is using the Advanced Custom Fields plugin.
One of those custom fields is "conference_date", which I want to show in the wp-admin/edit.php?post_type=conferences
path as a column just like "title" or "date" is being shown by default. Is there any way to do it using add_post_type_support()
or similar?
I already tried with this, which does not work:
add_action('init', 'show_conference_date', 100);
function show_conference_date() {
add_post_type_support('conferences', 'custom-fields');
}
EDIT:
Solved, just add (and edit) this to your functions.php
file:
// Add new table headings for "conferences" Custom Post Type
function set_columns_head_for_conferences($defaults) {
$defaults['conference_date'] = 'When';
$defaults['taxonomy-locations'] = 'Where';
$defaults['conference_live'] = 'Live now?';
return $defaults;
}
add_filter('manage_conferences_posts_columns', 'set_columns_head_for_conferences');
And set the column content like this:
// Example from: https://developer.wordpress.org/reference/hooks/manage_post-post_type_posts_custom_column/
// Set values to columns
function conferences_custom_column_values( $column, $post_id ) {
switch ( $column ) {
// in this example, "conferences" has custom fields called 'conference_date' and 'conference_live'
case 'conference_date' :
the_field('conference_date', $post_ID);
break;
case 'conference_live' :
if (get_field('conference_live', $post_ID)) { //This is a "boolean" in my ACF
$conference_live_human_readable = "Yes";
} else {
$conference_live_human_readable = "No";
}
echo $conference_live_human_readable;
break;
}
}
add_action( 'manage_conferences_posts_custom_column' , 'conferences_custom_column_values', 10, 2 );
If you want to change the new column order, use this:
function column_order($columns) {
$n_columns = array();
$move = 'conference_date'; // what to move
$before = 'conference_live'; // move before this
foreach($columns as $key => $value) {
if ($key==$before){
$n_columns[$move] = $move;
}
$n_columns[$key] = $value;
}
return $n_columns;
}
add_filter('manage_conferences_posts_columns', 'column_order', 100);
You can debug all the $before
options using return print_r($columns)
instead of return $n_columns
.
Default options for $before
are:
- 'cb' : checkbox for selecting post items for bulk actions
- 'title' : displays the post title as well as post action links (edit, quick edit, trash, view) based on user permissions
- 'author' : displays the username of the post author as a link to filter post by author
- 'categories' : displays the post categories as links to filter post by category
- 'tags' : displays the post tags as links to filter post by tags
- 'comments' : displays a comment icon with the number of comments as a permalink to manage the comments for that post
- 'date' : displays the date and status of the post
The last piece of code to order the new column is from: https://www.isitwp.com/change-wordpress-admin-post-columns-order/
Thanks to Alexander Holsgrove for its response.