Created
December 10, 2025 17:48
-
-
Save AndreasFurster/1b77f6690fb583e88a6b3b991507c876 to your computer and use it in GitHub Desktop.
Wordpress add admin columns from (ACF) meta fields
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
| <?php | |
| class AdminColumnManager | |
| { | |
| /** | |
| * Adds a custom column to a post type in the admin list table based on post meta. | |
| * | |
| * @param string $postType The post type to add the column to. | |
| * @param string $columnKey The key for the column. | |
| * @param string $columnLabel The label for the column. | |
| * @param callable|null $renderCallback Optional. A callback function to render the column content. If null, the column will display the post meta with the same key as $columnKey. | |
| */ | |
| public static function addMeta(string $postType, string $columnKey, string $columnLabel, ?callable $renderCallback = null) | |
| { | |
| add_filter("manage_edit-{$postType}_columns", function ($columns) use ($columnKey, $columnLabel) { | |
| // By default add before the date column | |
| $dateColumn = $columns['date']; | |
| unset($columns['date']); | |
| $columns[$columnKey] = $columnLabel; | |
| $columns['date'] = $dateColumn; | |
| return $columns; | |
| }); | |
| add_action("manage_{$postType}_posts_custom_column", function ($column, $postId) use ($columnKey, $renderCallback) { | |
| if ($column !== $columnKey) return; | |
| $value = get_post_meta($postId, $columnKey, true); | |
| if (is_null($renderCallback)) { | |
| echo esc_html($value ?? '—'); | |
| } else { | |
| echo esc_html(call_user_func($renderCallback, $value)); | |
| } | |
| }, 10, 2); | |
| } | |
| } |
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
| <?php | |
| AdminColumnManager::addMeta('projects', 'place', 'Place'); | |
| AdminColumnManager::addMeta('projects', 'date', 'Date', fn($value) => get_the_date('', $value)); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ideas: