How To Register A Custom Taxonomy In WordPress
A taxonomy is a way to group content. Categories and tags are the built-in taxonomies. A custom taxonomy lets you create your own groups, like project types, genres or brands, and attach them to any post type.
Register a hierarchical taxonomy, like categories
function lotgrafix_register_project_type() {
$labels = array(
'name' => __( 'Project Types', 'lotgrafix' ),
'singular_name' => __( 'Project Type', 'lotgrafix' ),
'add_new_item' => __( 'Add New Project Type', 'lotgrafix' ),
'search_items' => __( 'Search Project Types', 'lotgrafix' ),
);
register_taxonomy( 'lotgrafix_project_type', array( 'lotgrafix_portfolio' ), array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'rewrite' => array( 'slug' => 'project-type' ),
) );
}
add_action( 'init', 'lotgrafix_register_project_type' );
Register a non hierarchical taxonomy, like tags
Set 'hierarchical' => false and the editor shows a free text box instead of checkboxes.
Show the terms of a post
<?php
the_terms( get_the_ID(), 'lotgrafix_project_type', '<p class="lotgrafix-terms">', ', ', '</p>' );
?>
Get the terms as an array
$lotgrafix_terms = get_the_terms( get_the_ID(), 'lotgrafix_project_type' );
if ( $lotgrafix_terms && ! is_wp_error( $lotgrafix_terms ) ) {
foreach ( $lotgrafix_terms as $lotgrafix_term ) {
echo '<a href="' . esc_url( get_term_link( $lotgrafix_term ) ) . '">' . esc_html( $lotgrafix_term->name ) . '</a> ';
}
}
List all terms
$lotgrafix_all = get_terms( array(
'taxonomy' => 'lotgrafix_project_type',
'hide_empty' => true,
) );
Notes
- The taxonomy name can have a maximum of 32 characters.
- Save the permalinks after registering, like with custom post types.
- Use the template
taxonomy-lotgrafix_project_type.phpto customize the archive page.


