This reusable Higher Order Component handles asynchronous data fetching in React applications, making it ideal for fetching data from APIs on componen...
Back to Snippets
php
• 37 lines
JavaScript
JavaScript
PHP
Add Custom Admin Columns for a CPT
Add custom sortable admin columns to any post type — render post meta in the admin list table with proper escaping.
Snippet Stats
Lines
37
Characters
1,148
Read
2 mins
/**
* Add custom columns to the portfolio admin list.
*/
add_filter( 'manage_portfolio_posts_columns', function( array $columns ): array {
$new = array();
foreach ( $columns as $key => $label ) {
$new[ $key ] = $label;
if ( 'title' === $key ) {
$new['project_url'] = __( 'Project URL', 'theme-domain' );
$new['project_year'] = __( 'Year', 'theme-domain' );
}
}
return $new;
} );
/**
* Render the custom column content.
*/
add_action( 'manage_portfolio_posts_custom_column', function( string $column, int $post_id ): void {
switch ( $column ) {
case 'project_url':
$url = get_post_meta( $post_id, '_project_url', true );
echo $url ? '' . esc_html( $url ) . '' : '—';
break;
case 'project_year':
echo esc_html( get_post_meta( $post_id, '_project_year', true ) ?: '—' );
break;
}
}, 10, 2 );
/**
* Make the Year column sortable.
*/
add_filter( 'manage_edit-portfolio_sortable_columns', function( array $columns ): array {
$columns['project_year'] = 'project_year';
return $columns;
} );
Found an issue with this snippet? Help us improve by reporting it. Report it →
Related Snippets
View allThis React code snippet demonstrates a centralized global state management pattern using the Context API, simplifying state access and updates across ...
This code snippet provides a custom WordPress hook to update user profiles. It is useful when you need to synchronize user data with external services...