Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Adding a post type

Koen edited this page Sep 12, 2019 · 6 revisions

Post types are the core of the most WordPress websites, think of them as models. We're using a PostTypeAbstract with sensible defaults, which is extended per post type.

Creating a post type

  • Create a class in Grrr\PostTypes, extending PostTypeAbstract
  • Call the register function in functions.php

An example for a post type class could be:

namespace Grrr\PostTypes;

class Example extends PostTypeAbstract {

    protected $_type               = 'example';
    protected $_slug               = 'examples';
    protected $_name               = 'Examples';
    protected $_singular_name      = 'Example';
    protected $_icon               = 'dashicons-portfolio';

    protected $_args = [
        'public' => true,
        'has_archive' => true,
        'taxonomies' => [
            'example-type',
        ],
    ];

}

For all available arguments of the underlying register_post_type used, view the WordPress codex or code reference.

Common options

A few common configuration options for post types are:

  • No featured image: remove the thumbnail from the supports array and add it to your class.
  • Adding a taxonomy: add the taxonomy slug to the taxonomies array and add it to your class.
  • No single/detail page: set public to false. It will not have an archive page, but is queryable.
  • No archive/index page: set has_archive to false. It will still have a single/detail page, and is queryable.

Choosing an icon

You can choose an icon from the WordPress Dashicons, which will be visible in the CMS for the newly created post type.

Naming caveats

  • The registered name cannot be longer than 20 characters