Search
Close this search box.

Custom Taxonomy in WordPress (custom post type)

Amitpal Singh
Amitpal Singh
April 27, 2023

Custom Taxonomy in WordPress for custom post type

// Custom Taxonomay "Location" for custom post type "Books"

add_action( 'init', 'create_locations_nonhierarchical_taxonomy', 0 );
  
function create_locations_nonhierarchical_taxonomy() {
  
// Labels part for the GUI
  
  $labels = array(
    'name' => _x( 'Locations', 'taxonomy general name' ),
    'singular_name' => _x( 'Location', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Locations' ),
    'popular_items' => __( 'Popular Locations' ),
    'all_items' => __( 'All Locations' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Location' ), 
    'update_item' => __( 'Update Location' ),
    'add_new_item' => __( 'Add New Location' ),
    'new_item_name' => __( 'New Location Name' ),
    'separate_items_with_commas' => __( 'Separate locations with commas' ),
    'add_or_remove_items' => __( 'Add or remove locations' ),
    'choose_from_most_used' => __( 'Choose from the most used locations' ),
    'menu_name' => __( 'Locations' ),
  ); 
  
// Now register the non-hierarchical taxonomy like tag
 //you can change "books" to your custom post type
  register_taxonomy('locations','books',array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'location' ),
  ));
} 
Still need help on creating custom taxonomies? Please contact Amitpal Singh Click Here

More about Custom Taxonomies in WordPress:

Introduction: WordPress, one of the most popular content management systems (CMS) in the world, offers a wide range of features and functionalities to its users. One such powerful feature is custom taxonomy, which allows website owners to organize and classify their content in a highly flexible and personalized manner. In this article, we will delve into the world of custom taxonomy in WordPress, exploring its benefits, implementation, and practical use cases.

Understanding Taxonomy in WordPress: Before we dive into custom taxonomy, let’s briefly discuss the concept of taxonomy in WordPress. Taxonomy is a way of classifying and organizing content. By default, WordPress provides three built-in taxonomies: categories, tags, and post formats. However, these may not always be sufficient to meet the specific requirements of every website.

What is Custom Taxonomy? Custom taxonomy, as the name suggests, allows you to create and define your own taxonomies. It provides a flexible and efficient solution for organizing content beyond the default taxonomies. With custom taxonomy, you can create custom labels, hierarchies, and classifications that are tailored to your website’s unique needs.

Benefits of Custom Taxonomy:

  1. Improved Content Organization: Custom taxonomy empowers you to create a more structured and organized content hierarchy. It helps visitors easily navigate and find relevant content on your website.

  2. Enhanced User Experience: By implementing custom taxonomy, you can create intuitive and user-friendly interfaces, making it easier for visitors to browse through related content and discover relevant information.

  3. Flexible Content Management: Custom taxonomy offers a high degree of flexibility in managing and classifying your content. It allows you to assign multiple taxonomies to a single content item, providing granular control over how content is categorized and displayed.

Implementing Custom Taxonomy in WordPress: To implement custom taxonomy in WordPress, you can utilize a combination of built-in functions and custom code. Here’s a step-by-step guide to get you started:

  1. Define the Taxonomy: Use the register_taxonomy() function to define the taxonomy. Specify the name, labels, and settings for the taxonomy.

  2. Assign Taxonomy to Content: After defining the taxonomy, you can assign it to different content types such as posts, pages, or custom post types. This ensures that the taxonomy becomes available for classification and organization.

  3. Displaying Taxonomy on the Frontend: To showcase the custom taxonomy on your website’s frontend, you need to modify your theme or create custom templates. Utilize functions like get_terms() or get_the_terms() to retrieve and display the taxonomy terms associated with a specific content item.

Practical Use Cases for Custom Taxonomy:

  1. E-commerce Websites: Custom taxonomy can be used to create product categories, attributes, or tags, allowing customers to filter and find products based on specific criteria.

  2. News and Magazine Websites: Implementing custom taxonomy enables you to categorize articles by topics, authors, or regions, enhancing navigation and content discovery.

  3. Events and Listings Websites: Custom taxonomy can help classify events or listings by location, type, or date, making it easier for users to find relevant information.

Conclusion: Custom taxonomy is a powerful tool that expands the capabilities of WordPress, enabling website owners to create personalized and highly organized content structures. By implementing custom taxonomy, you can improve content management, enhance user experience, and make your website more intuitive and user-friendly. Embrace the potential of custom taxonomy and unlock a new level of flexibility and organization within your WordPress site.

Share this post:

custom taxonomy in wordpress

How to Attribute?

Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
for Example: Website, Social Media, Blogs, ebooks , newsletter, etc.
Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
Copied!

Got a Question? Check out our FAQ Section.

Your action, our appreciation

It encourage us to give you more valuable content on website.