concept-image / wp-custom-post-types
Register custom post types and taxonomies in WordPress.
Requires
- php: >=8.2
- concept-image/wp-query: ^0.0.2
- roots/acorn: >=4.3
Suggests
- concept-image/wp-make-cpt: >=1.0
README
Requirements
- PHP >= 8.0
- WordPress >= 6.7
- Roots Bedrock >= 1.26
- Roots Acorn >= 4.3
Installation
You can install this package with Composer:
composer require concept-image/wp-custom-post-types
Usage
This package provides a convenient way to create and manage custom post types and taxonomies in WordPress using a structured approach. It leverages the power of Roots Bedrock and Acorn to integrate seamlessly into your WordPress project.
Custom Post Types
To create a custom post type, you need to define a class in the app/CustomPostTypes
directory. The class should extend PostType
and have a protected static $name
property and a getArgs
method that returns the arguments for the custom post type registration.
Example:
namespace App\CustomPostTypes;
use ConceptImage\WpCustomPostTypes\PostType;
class Book extends PostType
{
protected static string $name = 'book';
public function getArgs(): array
{
return [
'label' => 'Books',
'public' => true,
'supports' => ['title', 'editor', 'thumbnail'],
'taxonomies' => ['genre'],
];
}
}
Custom Taxonomies
To create a custom taxonomy, you need to define a class in the app/Taxonomies
directory. The class should extend Taxonomy
and have a protected static $name
property, an $object_type
property, and a getArgs
method.
Example:
namespace App\Taxonomies;
use ConceptImage\WpCustomPostTypes\Taxonomy;
class Genre extends Taxonomy
{
protected static string $name = 'genre';
public array $object_type = ['book'];
public function getArgs(): array
{
return [
'label' => 'Genres',
'public' => true,
'hierarchical' => true,
];
}
}
Removing Base Slug from URLs
If you want to remove the base slug from your custom post type URLs (e.g., /book/my-book
becomes /my-book
), you can use the RemoveBaseSlug
attribute on your custom post type class:
use ConceptImage\WpCustomPostTypes\Attributes\RemoveBaseSlug;
#[RemoveBaseSlug]
class Book
{
...
}
Taxonomies
To create a custom taxonomy, you need to define a class in the app/Taxonomies
directory. The class should have a name
property, an object_type
property, and a getArgs
method that returns the arguments for the taxonomy registration.
Example:
namespace App\Taxonomies;
class Genre
{
public $name = 'genre';
public $object_type = ['book'];
public function getArgs(): array
{
return [
'label' => 'Genres',
'public' => true,
'hierarchical' => true,
];
}
}
Registering Custom Post Types and Taxonomies
The package automatically registers custom post types and taxonomies defined in the app/CustomPostTypes
and app/Taxonomies
directories, respectively. You don't need to manually register them in your theme or plugin.
Migrate from V2 to V3
Run following command :
wp acorn custom-post-types:migration
Changelog
Please refer to CHANGELOG for more information.
Contributing
Please refer to CONTRIBUTING for more information.
License
Please refer to LICENSE for more information.