jjgrainger / posttypes
Simple WordPress custom post types.
Installs: 546 092
Dependents: 15
Suggesters: 1
Security: 0
Stars: 373
Watchers: 20
Forks: 46
pkg:composer/jjgrainger/posttypes
Requires
- php: >=7.2
Requires (Dev)
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: 3.*
- szepeviktor/phpstan-wordpress: ^2.0
This package is auto-updated.
Last update: 2026-01-25 19:00:41 UTC
README
Modern PHP abstractions for WordPress post types and taxonomies.
Migrating from v2 to v3
Important: v3.0 is a breaking release. Existing v2 post type and taxonomy definitions will not work without modification. Please review the migration guide in the documentation on how to upgrade to version 3.
Requirements
Installation
Install with composer
Run the following in your terminal to install PostTypes with Composer.
$ composer require jjgrainger/posttypes
PostTypes uses PSR-4 autoloading and can be used with the Composer's autoloader. See Composer's basic usage guide for details on working with Composer and autoloading.
Basic Usage
Create a custom post type
Custom post types are defined as classes that extend the base PostType class. At a minimum, the name method must be implemented to define the post type slug. All other methods are optional and allow you to configure labels, options, taxonomies, admin columns, filters, and more as needed.
<?php use PostTypes\PostType; use PostTypes\Columns; class Book extends PostType { /** * Define the Post Type name. */ public function name(): string { return 'book'; } /** * Define the Post Type labels. */ public function labels(): array { return [ 'name' => __( 'Book', 'text-domain' ), 'singular_name' => __( 'Book', 'text-domain' ), 'menu_name' => __( 'Books', 'text-domain' ), 'all_items' => __( 'Books', 'text-domain' ), 'add_new' => __( 'Add New', 'text-domain' ), 'add_new_item' => __( 'Add New Book', 'text-domain' ), 'edit_item' => __( 'Edit Book', 'text-domain' ), 'new_item' => __( 'New Book', 'text-domain' ), 'view_item' => __( 'View Book', 'text-domain' ), 'search_items' => __( 'Search Books', 'text-domain' ), 'not_found' => __( 'No Books found', 'text-domain' ), 'not_found_in_trash' => __( 'No Books found in Trash', 'text-domain' ), 'parent_item_colon' => __( 'Parent Book', 'text-domain' ), ]; } /** * Define Post Type feature supports. */ public function supports(): array { return [ 'title', 'editor', 'thumbnail', 'custom-fields', ]; } /** * Define Taxonomies associated with the Post Type. */ public function taxonomies(): array { return [ 'genre', 'category', ]; } /** * Set the menu icon for the Post Type. */ public function icon(): string { return 'dashicons-book'; } /** * Set the admin post table filters. */ public function filters(): array { return [ 'genre', 'category', ]; } /** * Define the columns for the admin post table. */ public function columns(Columns $columns): Columns { // Remove the author and date column. $columns->remove( [ 'author', 'date' ] ); // Add a Rating column. $columns->add( 'rating', __( 'Rating', 'post-types' ) ); // Populate the rating column. $columns->populate( 'rating', function( $post_id ) { echo get_post_meta( $post_id, 'rating', true ); } ); return $columns; } }
Register a custom post type
Once the custom post type class is created it can be registered to WordPress by instantiating and call the register method.
// Instantiate the Book PostType class. $book = new Book; // Register the Book PostType to WordPress. $book->register();
Notes
- The full documentation can be found online at posttypes.jjgrainger.co.uk
- Licensed under the MIT License
- Maintained under the Semantic Versioning Guide
Author
Joe Grainger