Skip to main content

Create a Custom Admin View for a CPT Type

Author

Robert Loncaric

Timestamps

Created: 14 August 2024
Modified: 14 August 2024

If you need to have a custom view in admin area for a speecific CPT post type, here's a ready solution.

Steps

Just copy this to functions.php and change the fields you need.

////////// BEGIN CUSTOM ADMIN VIEW FOR A CPT TYPE //////////
// Add custom columns to the admin list view for the 'podatak' CPT
<?php
function custom_podatak_columns($columns) {
    // Leave default columns and add new ones
    $columns['fond'] = __('Fond');
    $columns['datum'] = __('Datum');
    $columns['nav'] = __('NAV');
    $columns['imovina'] = __('Imovina');
    
    return $columns;
}
add_filter('manage_podatak_posts_columns', 'custom_podatak_columns');

// Render data from ACF fields (here they're in a group called 'podatak')
function custom_podatak_column_content($column, $post_id) {
    if ('fond' === $column) {
        echo esc_html(get_field('podatak_fond', $post_id));
    }
    if ('datum' === $column) {
        echo esc_html(get_field('podatak_datum', $post_id));
    }
    if ('nav' === $column) {
        echo esc_html(get_field('podatak_nav', $post_id));
    }
    if ('imovina' === $column) {
        echo esc_html(get_field('podatak_imovina', $post_id));
    }
}
add_action('manage_podatak_posts_custom_column', 'custom_podatak_column_content', 10, 2);
?>
////////// END CUSTOM ADMIN VIEW FOR A CPT TYPE //////////