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');
Artikel von:
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.
The blog on WordPress terms with PHP provides a clear and concise explanation of key concepts and functions used in WordPress development. The focus on how these terms relate to PHP helps bridge the gap between WordPress and backend programming. The practical examples and definitions make it an excellent resource for developers looking to deepen their understanding of WordPress and PHP integration. Great job on making complex topics more accessible and understandable!
The blog on WordPress terms with PHP provides a clear explanation of key concepts and functions used in WordPress development. It effectively bridges the gap between PHP programming and WordPress-specific terminology, making it easier for developers to understand and apply these terms in their projects. The practical examples and definitions are valuable for anyone looking to deepen their knowledge of WordPress development. Great job on demystifying these important concepts!
The blog on WordPress terms with PHP provides a helpful overview of key PHP concepts and how they relate to WordPress development. The explanations of terms and code examples are valuable for both novice and experienced developers looking to deepen their understanding of WordPress customization and functionality. This post serves as a useful reference for anyone working with WordPress and PHP. Great job on breaking down complex topics into accessible and practical information!
The blog on WordPress terms with PHP offers a clear and concise explanation of PHP concepts in the context of WordPress development. The detailed breakdown of terms and their practical applications is excellent for both new and experienced developers. It provides valuable insights into how PHP is used to extend WordPress functionalities. This guide is a great resource for improving your WordPress coding skills and understanding the underlying PHP code. Thanks for the informative and well-organized post!