Posted in WordPress

Create a custom taxonomy in WordPress

Create a custom taxonomy in WordPress Posted on November 22, 2015Leave a comment

WordPress has four built-in taxonomies, they are Category, Tag, Link Category and Post Formats but in some cases, we have to create our custom taxonomies for specific reasons. The function below will support us to create a custom taxonomy in WordPress:

  • $taxonomy: (string) (required) The name of the taxonomy.
  • $object_type: (array/string) (required) Name of the post type for the taxonomy object.
  • $args: (array/string) (optional) An array of Arguments.
    • label (string) (optional) A plural descriptive name for the taxonomy marked for translation.
    • labels (array) (optional) An array of labels for this taxonomy.
      • name: general name for the taxonomy, usually plural.
      • singular_name: name for one object of this taxonomy.
      • menu_name: the menu name text.
      • all_items: the all items text.
      • edit_item: the edit item text.
      • view_item: the view item text.
      • update_item: the update item text.
      • add_new_item: the add new item text.
      • new_item_name: the new item name text.
      • parent_item: the parent item text.
      • parent_item_colon: The same as parent_item, but with colon : in the end.
      • search_items: the search items text.
      • popular_items: the popular items text. This string is not used on hierarchical taxonomies.
      • separate_items_with_commas: the separate item with commas text used in the taxonomy meta box. This string is not used on hierarchical taxonomies.
      • add_or_remove_items: the add or remove items text and used in the meta box when JavaScript is disabled. This string is not used on hierarchical taxonomies.
      • choose_from_most_used: the choose from most used text used in the taxonomy meta box. This string is not used on hierarchical taxonomies.
      • not_found: the text displayed via clicking ‘Choose from the most used tags’ in the taxonomy meta box when no tags are available and the text used in the terms list table when there are no items for a taxonomy.
    • public (boolean) (optional) If the taxonomy should be publicly queryable.
    • show_ui (boolean) (optional) Whether to generate a default UI for managing this taxonomy.
    • show_in_menu (boolean) (optional) Where to show the taxonomy in the admin menu. show_ui must be true.
    • show_in_nav_menus (boolean) (optional) true makes this taxonomy available for selection in navigation menus.
    • show_tagcloud (boolean) (optional) Whether to allow the Tag Cloud widget to use this taxonomy.
    • show_in_quick_edit (boolean) (optional) Whether to show the taxonomy in the quick/bulk edit panel.
    • meta_box_cb (callback) (optional) Provide a callback function name for the meta box display.
    • show_admin_column (boolean) (optional) Whether to allow automatic creation of taxonomy columns on associated post-types table.
    • description (string) (optional) Include a description of the taxonomy.
    • hierarchical (boolean) (optional) Is this taxonomy hierarchical (have descendants) like categories or not hierarchical like tags.
    • update_count_callback (string) (optional) A function name that will be called when the count of an associated $object_type, such as post, is updated. Works much like a hook.
    • query_var (boolean or string) (optional) False to disable the query_var, set as string to use custom query_var instead of default which is $taxonomy, the taxonomy’s “name”.
    • rewrite (boolean/array) (optional) Set to false to prevent automatic URL rewriting a.k.a. “pretty permalinks”. Pass an $args array to override default URL settings for permalinks as outlined below:
      • slug: Used as pretty permalink text (i.e. /tag/).
      • with_front: allowing permalinks to be prepended with front base.
      • hierarchical: true or false allow hierarchical urls.
      • ep_mask: (Required for pretty permalinks) Assign an endpoint mask for this taxonomy – defaults to EP_NONE. If you do not specify the EP_MASK, pretty permalinks will not work.
    • capabilities (array) (optional) An array of the capabilities for this taxonomy.
      • manage_terms: ‘manage_categories’
      • edit_terms: ‘manage_categories’
      • delete_terms: ‘manage_categories’
      • assign_terms: ‘edit_posts’
    • sort (boolean) (optional) Whether this taxonomy should remember the order in which terms are added to objects.
    • _builtin (boolean) (not for general use) Whether this taxonomy is a native or “built-in” taxonomy.

And after built the function that creates a custom taxonomy, we have to hook it to init action. Here is a basic example with a hierarchical taxonomy:

This article is very basic but I hope you like it. Thanks!

Leave a Reply

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