Created
August 22, 2023 22:31
-
-
Save jnicol/c7593b7d53db5b0932177a53f2fa3165 to your computer and use it in GitHub Desktop.
Display advanced custom fields values in WordPress admin columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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