Skip to content

Registering Custom Post Types & Taxonomies

Brian McCoy edited this page Feb 23, 2018 · 3 revisions

Alloy Core provides wrapper functions to the WordPress register_post_type and register_taxonomy functions. These should be defined under app/content/content-types.php in your theme.

Registering a post type

You can use the core_post_type function to register a new post type. For example:

core_post_type( 'Case Studies' )

This will register a post type called "Case Studies" that you can query posts from using the slug case-studies. If you wanted to change the slug you could use an optional second parameter, which you can pass any register_post_type argument to:

core_post_type( 'Case Studies', [
	'slug' => 'case-study'
] );

core_post_type does change one default that register_post_type has. It sets the "public" option to true, since most of the time you want your post type to be public. You could easily change this back. Going off of our previous example:

core_post_type( 'Case Studies', [
	'slug' => 'case-study',
	'public' => 'false'
] );

Registering a taxonomy

To register a taxonomy it's much like registering a post type. You just have to define a label and the post type. Going off our above example you might register a taxonomy like:

core_taxonomy( 'Project Type', 'case-study' );

There's a third optional parameter where you can pass any parameter from register_taxonomy in. core_taxonomy changes two register_taxonomy defaults: It sets public and hierarchical to true. Let's say you don't want a hierarchical taxonomy, you want it to behave like tags:

core_taxonomy( 'Project Type', 'case-study', [
	'hierarchical' => false
] );

Clone this wiki locally