Create Custom Post Type and Custom Taxonomy

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

  1. Include the library
  2. Extend the library factory class
  3. Define the post type arguments in the setUp() method
  4. Register a custom taxonomy
  5. Instantiate the extended class

Screenshot

Create a Custom Post Type and Custom Taxonomy

Include the library

The library (framework) file needs to be loaded. Change the library path as you need.

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.

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.

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.

  1. (string) the taxonomy slug.
  2. (array) the taxonomy arguments. For the specifications, see the Arguments section of the register_taxonomy() method.

Instantiate the extended class

Finally, we instantiate the class. Unless it is instantiated, it will do nothing.

Set the parameters as follows.

  1. (sting|array) the post type slug(s). The character length must be within 25 characters.
  2. (optional, array) the post type arguments. If omitted, set the post type argument in the setUp() method.
  3. (optional, string) the caller script path.
  4. (optional, string) the text domain for translation.

Code

Try the code below and post comments if you get a problem.

Leave a Reply

Your email address will not be published. Required fields are marked *