Skip to content

Instantly share code, notes, and snippets.

@AndreasFurster
Created December 10, 2025 17:48
Show Gist options
  • Select an option

  • Save AndreasFurster/1b77f6690fb583e88a6b3b991507c876 to your computer and use it in GitHub Desktop.

Select an option

Save AndreasFurster/1b77f6690fb583e88a6b3b991507c876 to your computer and use it in GitHub Desktop.
Wordpress add admin columns from (ACF) meta fields
<?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);
}
}
<?php
AdminColumnManager::addMeta('projects', 'place', 'Place');
AdminColumnManager::addMeta('projects', 'date', 'Date', fn($value) => get_the_date('', $value));
@AndreasFurster
Copy link
Author

AndreasFurster commented Dec 10, 2025

Ideas:

  • addMetaDate
  • addMetaImage
  • addCustom
  • add...
  • Provide default values
  • A way to remove previously added fields?
  • "fluent" api Laravel style (nice with the unreleased php 8.5 pipe operator?)
    AdminColumnManager::post_type('projects')
      ->add('date')
      ->label('Finished date')
      ->after('title')
      ->using(fn($value) => get_the_date('', $value));
  • Add bulk edit options
  • Add filter options
  • Add searchable options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment