Custom Post Type and Custom Taxonomy
What Is a Post Type?
WordPress assigns a type to posts internally and its type is called a post type. Every one of posts has a type and its slug. For example, posts you create from Dashboard -> Posts has the slug of post and pages you create from Dashboard -> Pages has the slug of page. You know their contents are displayed separately in the UI so you can tell this is a post and that one is a page. Like so, you can add your own version of a post type to let the site user create a certain type of posts. It’s call a custom post type.
Why Use a Custom Post Type?
With a combination with post meta boxes, we can create a powerful plugin by using a post as an options container. You can make a certain post type invisible to the site visitor and you store options in a post meta field associated with a post of the custom post type you create.
Registering a Custom Post Type
To register a custom post type, we normally use the WordPress core function, register_post_type(). Admin Page Framework also provides a means to register a post type and facilitates the customization of columns in the post listing table, registering a custom taxonomy, inserting custom text in the footer, and modifying the contents of the article page etc.
What Is a Taxonomy?
WordPress has a feature to let the user assign certain words to a post so that he/she can know what kind of post it is. By default, posts you access from Dashboard -> Posts have a taxonomy named Category and you can add terms via Dashboard -> Posts -> Categories. You can add your own version of a category and it is called a Custom Taxonomy.
Why Need a Custom Taxonomy?
If you create a custom post type which lets your users have thousands of posts, it becomes useful to have a taxonomy. You can query posts more easily if there is an associated term.
Let’s create it
In this tutorial, to see the overall workflow, we just create a custom post type and a custom taxonomy only. In the following tutorials, we add a meta box that lets the user set custom meta field data as plugin options and display the result in the front end.
Steps
- Include the library
- Extend the library factory class
- Define the post type arguments in the setUp() method
- Register a custom taxonomy
- Instantiate the extended class
Screenshot
Include the library
The library (framework) file needs to be loaded. Change the library path as you need.
13 |
include( dirname( dirname( __FILE__ ) ) . '/library/apf/admin-page-framework.php' ); |
Extend the library class
We extend the AdminPageFramework_PostType factory class. Pick a unique class name consisting of alpha-numeric characters with underscores. We use APF_Tutorial_ExamplePostType here as an example.
1 2 3 |
class APF_Tutorial_ExamplePostType extends AdminPageFramework_PostType { // our code goes here… } |
Define the post type arguments in the setUp() method
In the setUp() method, we define post type arguments. It is basically the same one passed to the register_post_type() function except a few framework specific arguments.
For the supported arguments built into the WordPress core, refer to the linked page of the register_post_type(). In addition to that, the below is the framework specific arguments.
- labels – (array) holds label arguments.
- plugin_listing_table_title_cell_link – (string) The text label that appears in the plugin title cell of the plugin listing table in the admin area (plugins.php). Default: Manage.
- screen_icon – (string) this is for WordPress v3.7.1 or below. Set a 32 x 23 pixel image url or path.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public function setUp() { $this->setArguments( array( // argument - for the array structure, refer to http://codex.wordpress.org/Function_Reference/register_post_type#Arguments 'labels' => array( 'name' => 'Tutorial Example', 'add_new_item' => __( 'Example Post', 'admin-page-framework-tutorial' ), 'plugin_action_link' => __( 'Tutorial Example Custom Post Type', 'admin-page-framework-tutorial' ), // (framework specific key). [3.0.6+] ), 'supports' => array( 'title', 'editor' ), // e.g. array( 'title', 'editor', 'comments', 'thumbnail', 'excerpt' ), 'public' => true, 'menu_icon' => version_compare( $GLOBALS['wp_version'], '3.8', '>=' ) ? 'dashicons-wordpress' : plugins_url( 'asset/image/wp-logo_16x16.png', APFDEMO_FILE ), // (framework specific key) this sets the screen icon for the post type for WordPress v3.7.1 or below. 'screen_icon' => dirname( APFDEMO_FILE ) . '/asset/image/wp-logo_32x32.png', // a file path can be passed instead of a url, plugins_url( 'asset/image/wp-logo_32x32.png', APFDEMO_FILE ) ) ); |
Register a custom taxonomy
Custom taxonomies are for categorizing posts. By default, a custom post type does not have that functionality. So we add a custom taxonomy associated with the post type. We can register a custom taxonomy in the setUp() method. For that, we use the addTaxnomy() method.
The parameters are as follows.
- (string) the taxonomy slug.
- (array) the taxonomy arguments. For the specifications, see the Arguments section of the register_taxonomy() method.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
$this->addTaxonomy( 'apf_tutorial_example_taxonomy', // taxonomy slug array( // argument - for the argument array keys, refer to : http://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments 'labels' => array( 'name' => __( 'Tutorial Taxonomy', 'admin-page-framework-tutorial' ), 'add_new_item' => __( 'Add New Taxonomy', 'admin-page-framework-tutorial' ), 'new_item_name' => __( 'New Taxonomy', 'admin-page-framework-tutorial' ) ), 'show_ui' => true, 'show_tagcloud' => false, 'hierarchical' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'show_table_filter' => true, // framework specific key 'show_in_sidebar_menus' => true, // framework specific key ) ); |
Instantiate the extended class
Finally, we instantiate the class. Unless it is instantiated, it will do nothing.
Set the parameters as follows.
- (sting|array) the post type slug(s). The character length must be within 25 characters.
- (optional, array) the post type arguments. If omitted, set the post type argument in the setUp() method.
- (optional, string) the caller script path.
- (optional, string) the text domain for translation.
61 |
new APF_Tutorial_ExamplePostType( 'my_apf_post_type' ); |
Code
Try the code below and post comments if you get a problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?php /** * Plugin Name: Admin Page Framework Tutorial 12 - Create a Custom Post Type and Custom Taxonomy * Plugin URI: http://en.michaeluno.jp/admin-page-framework * Description: Creates a custom post type and a custom taxonomy that belongs to it. * Author: Michael Uno * Author URI: http://michaeluno.jp * Version: 1.0.1 * Requirements: PHP 5.2.4 or above, WordPress 3.4 or above. Admin Page Framework 3.0.6 or above */ // Include the library file. Set your file path here. include( dirname( dirname( __FILE__ ) ) . '/library/apf/admin-page-framework.php' ); class APF_Tutorial_ExamplePostType extends AdminPageFramework_PostType { /** * Automatically called with the 'wp_loaded' hook. */ public function setUp() { $this->setArguments( array( // argument - for the array structure, refer to http://codex.wordpress.org/Function_Reference/register_post_type#Arguments 'labels' => array( 'name' => 'Tutorial Example', 'add_new_item' => __( 'Example Post', 'admin-page-framework-tutorial' ), 'plugin_action_link' => __( 'Tutorial Example Custom Post Type', 'admin-page-framework-tutorial' ), // (framework specific key). [3.0.6+] ), 'supports' => array( 'title', 'editor' ), // e.g. array( 'title', 'editor', 'comments', 'thumbnail', 'excerpt' ), 'public' => true, 'menu_icon' => version_compare( $GLOBALS['wp_version'], '3.8', '>=' ) ? 'dashicons-wordpress' : plugins_url( 'asset/image/wp-logo_16x16.png', APFDEMO_FILE ), // (framework specific key) this sets the screen icon for the post type for WordPress v3.7.1 or below. 'screen_icon' => dirname( APFDEMO_FILE ) . '/asset/image/wp-logo_32x32.png', // a file path can be passed instead of a url, plugins_url( 'asset/image/wp-logo_32x32.png', APFDEMO_FILE ) ) ); $this->addTaxonomy( 'apf_tutorial_example_taxonomy', // taxonomy slug array( // argument - for the argument array keys, refer to : http://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments 'labels' => array( 'name' => __( 'Tutorial Taxonomy', 'admin-page-framework-tutorial' ), 'add_new_item' => __( 'Add New Taxonomy', 'admin-page-framework-tutorial' ), 'new_item_name' => __( 'New Taxonomy', 'admin-page-framework-tutorial' ) ), 'show_ui' => true, 'show_tagcloud' => false, 'hierarchical' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'show_table_filter' => true, // framework specific key 'show_in_sidebar_menus' => true, // framework specific key ) ); } } new APF_Tutorial_ExamplePostType( 'my_apf_post_type' ); |