WordPress taxonomies are a way of grouping posts or custom post types. We already looked at taxonomies while discussing database tables and templates in previous chapters. In this section, we are going to look at how taxonomies fit into custom post types and their implementation. In this scenario, we are building a property listing site. We can use taxonomies to group properties based on the property type. Let's begin by adding the following action to the constructor of the WQCPT_Model_Property class:
$this->property_category_taxonomy = 'wqcpt_property_listing_type';
add_action( 'init', array( $this, 'create_property_custom_taxonomies' ) );
As with custom post types, we have to use the init action to register taxonomies for WordPress. Let's consider the implementation of the create_property_custom_taxonomies function:
public function create_property_custom_taxonomies() {
$category_taxonomy = $this->property_category_taxonomy;
$singular_name = __('Property Listing Type','wqcpt');
$plural_name = __('Property Listing Types','wqcpt');
register_taxonomy(
$category_taxonomy,
$this->post_type,
array(
'labels' => array(
'name' => sprintf( __( '%s ', 'wqcpt' ) , $singular_name),
'singular_name' => sprintf( __( '%s ', 'wqcpt' ) , $singular_name),
'search_items' => sprintf( __( 'Search %s ', 'wqcpt' ) , $singular_name),
),
'hierarchical' => true,
)
);
}
We start the function by defining the taxonomy and the necessary labels, similar to the custom post types. Then, we can call the register_taxonomy function with the necessary parameters to create the taxonomy for property types. Let's take a look at the parameters in detail:
- taxonomy: This is the first parameter, where we have to pass the name for the taxonomy. We can only use lowercase letters and characters. We have specified it as wqcpt_property_type, with a prefix to make it unique from other plugins.
- object type: This is the second parameter, where we have to assign the post types that will use this taxonomy. In this scenario, we have specified wqcpt_property as the post type. We can use an array in scenarios where we have multiple post types. However, using a taxonomy in multiple post types makes archive pages as a mix of different post type entries. It might be difficult to handle archive pages with multiple post types due to different designs and data. So, it's ideal to use separate taxonomies for each post type, unless all post types contain similar design and data in archive pages.
- arguments: This is the third parameter where we can pass different settings from the list of available options. In this scenario, we have used the labels for property listing types and hierarchical options. The hierarchical setting defines whether the taxonomy should act as a tag or category. The default value is false, making it act as a tag. We have used it as a category by setting it to true. There are many other settings similar to the custom post type registration process. You can view more details about all the available arguments at https://codex.wordpress.org/Function_Reference/register_taxonomy.
Once this code is used, you will see a new taxonomy added to the Property menu. You can create properties and assign property listing types to categorize the properties based on your needs. In real-world requirements, you have to match the taxonomy needs of each custom post type with categories or tags depending on the functionality.