Skip to content

Instantly share code, notes, and snippets.

@AnadarProSvcs
Last active December 19, 2023 18:57
Show Gist options
  • Save AnadarProSvcs/dc01b11471bf51a768c8758f4d376602 to your computer and use it in GitHub Desktop.
Save AnadarProSvcs/dc01b11471bf51a768c8758f4d376602 to your computer and use it in GitHub Desktop.
An over view of the WP_Term object from WordPress. WP_Term objects are often returned by functions like get_terms and get_term, and provide a convenient way to access the properties of individual terms. #wordpress

WordPress WP_Term Class Reference

Overview

WP_Term is a class in WordPress that represents terms within taxonomies such as categories, tags, or custom taxonomies.

Key Properties

  • term_id: The ID of the term.
  • name: The name of the term.
  • slug: The slug of the term, used in URLs.
  • term_group: The group that the term belongs to.
  • term_taxonomy_id: The ID of the term in the term_taxonomy table.
  • taxonomy: The taxonomy of the term (e.g., category, post_tag).
  • description: The description of the term.
  • parent: The ID of the parent term (for hierarchical taxonomies).
  • count: The number of posts associated with the term.

Common Functions Associated with WP_Term

  • get_term(): Retrieves a term given its ID and taxonomy.
  • get_terms(): Retrieves terms in a given taxonomy or list of taxonomies.
  • wp_insert_term(): Adds a new term to the database.
  • wp_update_term(): Updates a term in the database.
  • wp_delete_term(): Removes a term from the database.

Usage Example

$categories = get_terms(array(
    'taxonomy' => 'category',
    'hide_empty' => false,
));

foreach ($categories as $category) {
    echo $category->name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment