How to manipulate, create, update and delete WordPress Terms with PHP

Marc Wagner, February 6, 2022

Terms can be created, queried and edited via PHP. WordPress provides functions for this purpose.

Query a WordPress term with PHP #

To retrieve a term you can use the function get_term($term_id, $taxonomy). For this you only need the ID and the taxonomy.

$term_id = 10;
$taxonomy = "post_tag";

/**
 * @var \WP_Term $Term
 */
$Term = get_term($term_id, $taxonomy);

Query a WordPress term by slug, name or term ID #

Via get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = ‘raw’) you can also query a term by its slug, name or ID.

/**
 * Query WordPress Term with the Slug
 * @var \WP_Term|array|false $Term
 */
$Term = get_term_by("slug", "i-am-a-slug", "post_tag");

/**
 * Query WordPress Term with the Name
 * @var \WP_Term|array|false $Term
 */
$Term = get_term_by("name", "i am a name", "post_tag");

/**
 * Query WordPress Term with the Term ID
 * @var \WP_Term|array|false $Term
 */
$Term = get_term_by("term_id", 10, "post_tag");

Query all WordPress terms from one taxonomy #

Using get_terms($args) you can query all terms of a taxonomy.

$args = array(
   'taxonomy' => 'post_tag',
   'hide_empty' => false
);

/**
 * @var array<\WP_Term> $Terms
 */
$Terms = get_terms($args);

Inserting a new WordPress term at a taxonomy #

New terms can be added via wp_insert_term($term, $taxonomy, $args = array()). If the term already exists, it will be updated.

/**
 * Arguments can be specified optionally
 */
$args = array(
   'description' => 'Description',
   'parent' => 0,
   'slug' => ''
);

/**
 * Create new term in the taxonomy post_tag
 */
$term_id = wp_insert_term('I am a new term', 'post_tag', $args);

Delete a term on WordPress via PHP #

Terms can also be removed quickly and easily via PHP. For this purpose, the function wp_delete_term($term_id,$taxonomy) exists.

/**
 * Deletes the term with term ID 10 in the taxonomy post_tag
 */
$result = wp_delete_term(10, 'post_tag');

Query meta data of a term #

Like posts, terms can also contain metadata. These can be retrieved via get_term_meta($term_id, $meta_key, $single).

/**
 * Returns a single value
 * @var string|int|mixed $my_meta_data
 */
$my_meta_data = get_term_meta(10,'my_meta_data', true);

/**
 * Returns an array
 * @var array $meine_meta_daten
 */
$my_meta_data = get_term_meta(10,'my_meta_data');

Deleting metadata of a term in WordPress #

Metadata of terms can be deleted in WordPress via delete_term_meta($term_id, $meta_key, $meta_value = ”).

$result = delete_term_meta(10, 'my_meta_data');

Update/create metadata of a term via PHP #

To update already existing metadata of a term the function update_term_meta($term_id, $meta_key, $meta_value, $prev_value = ”) is used. If the data does not exist yet, it will be created.

$result = update_term_meta(10, 'my_meta_data', 'this will be outputed');
Avatar of Marc Wagner
Marc Wagner

Hi Marc here. I'm the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.

Similar Topics

Comments

Leave A Comment

Title