Customize the WordPress Custom Post Type List Using extended-custom-post-types

José Debuchy

Nov 19, 2023 | 3 min to read |

WordPress

Creating Post Types in WordPress is often a tedious process. On top of that, many times we have to rely on plugins or additional features within the admin panel to gain control over the columns or the way these post types are displayed.

Extended Custom Post Types is a very popular package on GitHub that, as its name suggests, extends useful functionality when creating Custom Post Types in WordPress.

How to install it

We assume we are using Sage 10, as we have throughout the series. Inside the theme folder, we install the library using Composer.

composer require johnbillion/extended-cpts

How to use it

The package allows you to create both post types and taxonomies directly from code.

Post Types

In our example, we’ll create a post type for restaurants. Just like WordPress provides the register_post_type() function, Extended CPTs offers a very similar method.

add_action('init', function () {
    register_extended_post_type('restaurant');
});

This is the most basic way to use the package.

Taxonomies

To create taxonomies, the register_taxonomy() function is extended.

add_action('init', function () {
    register_extended_post_type('restaurant');
    register_extended_taxonomy('cuisine', 'restaurant');
});

These are the default options the package uses as the third parameter. One of the most notable is setting hierarchical to true.

'public'            => true,
'show_ui'           => true,
'hierarchical'      => true,
'query_var'         => true,
'exclusive'         => false,
'allow_hierarchy'   => false,
'meta_box'          => null,
'dashboard_glance'  => false,
'checked_ontop'     => null,
'admin_cols'        => null,
'required'          => false,

Labels

If the project language is English, singular and plural labels are generated automatically.

This link shows all available WordPress labels.

If we want to add custom labels, we can do so using the third argument of the function as an array.

add_action('init', function () {
    register_extended_post_type('restaurant',
        [],
        [
            'singular' => 'Restaurant',
            'plural'   => 'Restaurants',
        ]
    );
});

Columns

This is probably one of the most interesting features of the library. It allows us to control which columns are displayed in the admin list view for a custom post type.

We work with the second argument of the function, using an array with the admin_cols key.

add_action('init', function () {
    register_extended_post_type('restaurant',
        [
            'admin_cols' => [
                ...
            ]
        ],
        ...
    );
});

Taxonomies

For the cuisine taxonomy we created, we need to specify the taxonomy name and optionally a title.

'admin_cols' => [
    'cuisine' => [
        'title' => 'Cuisine',
        'taxonomy' => 'cuisine'
    ],
]

Featured image

Using featured_image, we specify the featured image size. Width and height can also be defined.

'admin_cols' => [
    'featured_image' => [
        'title'          => 'Featured Image',
        'featured_image' => 'thumbnail',
        'width'          => 160,
        'height'         => 90,
    ],
]

ACF fields and custom functions

To display ACF fields or run custom logic, we can use the following approach.

'admin_cols' => [
    'address' => [
        'title'    => 'Address',
        'function' => function() {
            echo get_field('address');
        },
    ],
]

Dates

Using the post_field parameter, we can use post_date, post_date_gmt, post_modified, or post_modified_gmt. The display format can be customized following these rules.

Summary

As we’ve seen, this library makes it very easy and flexible to manage the columns and filters displayed in the Custom Post Type list within the WordPress admin panel. This can be extremely useful for content-heavy sites, where content managers need better tools to find and manage specific entries.

In addition to columns and filters, the library includes many other features that we skipped for brevity, but they can be explored in the official documentation: https://github.com/johnbillion/extended-cpts/wiki.

Notably, front-end features such as the following are also available:

  • Custom permalink structures
  • Custom query vars for sorting and filtering

Author

José Debuchy