Skip to content

Instantly share code, notes, and snippets.

@jnicol
Created August 22, 2023 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnicol/c7593b7d53db5b0932177a53f2fa3165 to your computer and use it in GitHub Desktop.
Save jnicol/c7593b7d53db5b0932177a53f2fa3165 to your computer and use it in GitHub Desktop.
Display advanced custom fields values in WordPress admin columns
/*
* Add columns to exhibition post list
*/
function add_acf_columns ( $columns ) {
return array_merge ( $columns, array (
'start_date' => __ ( 'Starts' ),
'years' => __ ( 'Years' )
) );
}
add_filter ( 'manage_exhibition_posts_columns', 'add_acf_columns' );
/*
* Add columns to exhibition post list
*/
function exhibition_custom_column ( $column, $post_id ) {
switch ( $column ) {
case 'start_date':
echo get_post_meta ( $post_id, 'start_date', true );
break;
case 'years':
// repeater field
$years = get_post_meta ( $post_id, 'years', true );
for ($i=0; $i<$years; $i++) {
$meta_key = 'years_'.$i.'_year';
$sub_field_value = get_post_meta($post_id, $meta_key, true);
if ($i > 0) {
echo ', ';
}
echo $sub_field_value;
}
break;
}
}
add_action ( 'manage_exhibition_posts_custom_column', 'exhibition_custom_column', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment