Taxonomy term management helps organize content by grouping items with labels called terms. It makes finding and sorting content easier.
Taxonomy term management in Wordpress
<?php // Get terms in a taxonomy $terms = get_terms(array( 'taxonomy' => 'category', 'hide_empty' => false, )); // Add a new term wp_insert_term('New Term', 'category'); // Update a term wp_update_term($term_id, 'category', array('name' => 'Updated Name')); // Delete a term wp_delete_term($term_id, 'category'); ?>
get_terms() fetches terms from a taxonomy like categories or tags.
wp_insert_term(), wp_update_term(), and wp_delete_term() manage terms by adding, changing, or removing them.
<?php // Get all categories including empty ones $categories = get_terms(array('taxonomy' => 'category', 'hide_empty' => false)); ?>
<?php // Add a new tag term wp_insert_term('Featured', 'post_tag'); ?>
<?php // Rename a category wp_update_term(12, 'category', array('name' => 'Updated Category')); ?>
<?php // Remove a tag wp_delete_term(34, 'post_tag'); ?>
This code lists all categories, then adds a new category called 'My New Category'. It shows success or error messages.
<?php // Example: List all categories and add a new one // Get all categories $categories = get_terms(array('taxonomy' => 'category', 'hide_empty' => false)); // Print category names foreach ($categories as $cat) { echo "Category: " . $cat->name . "\n"; } // Add a new category $new_term = wp_insert_term('My New Category', 'category'); if (is_wp_error($new_term)) { echo "Error adding category: " . $new_term->get_error_message() . "\n"; } else { echo "Added new category with ID: " . $new_term['term_id'] . "\n"; } ?>
Always check for errors when adding or updating terms using is_wp_error().
Term IDs are important to update or delete specific terms.
Use hide_empty => false to see all terms, even if no content uses them yet.
Taxonomy terms group content for easy organization and filtering.
Use WordPress functions like get_terms() and wp_insert_term() to manage terms.
Always handle errors and know term IDs when updating or deleting terms.
Practice
Solution
Step 1: Understand taxonomy terms role
Taxonomy terms are used to group content like posts or custom post types for better organization.Step 2: Compare with other options
User accounts, theme settings, and backups are unrelated to taxonomy terms.Final Answer:
To group and organize content for easier filtering and display -> Option DQuick Check:
Taxonomy terms = content grouping [OK]
- Confusing taxonomy terms with user roles
- Thinking taxonomy terms manage theme options
- Assuming taxonomy terms handle backups
Solution
Step 1: Identify the correct function name
The official WordPress function to add a taxonomy term iswp_insert_term().Step 2: Check function parameters
The function takes the term name and taxonomy slug as parameters, matching wp_insert_term('Term Name', 'taxonomy_name');.Final Answer:
wp_insert_term('Term Name', 'taxonomy_name'); -> Option AQuick Check:
Insert term function = wp_insert_term() [OK]
- Using non-existent functions like insert_term
- Omitting the 'wp_' prefix
- Confusing with functions for posts or users
$terms = get_terms(['taxonomy' => 'genre', 'hide_empty' => false]);
foreach ($terms as $term) {
echo $term->name . ', ';
}Solution
Step 1: Understand get_terms() output
The function returns an array of term objects for the 'genre' taxonomy, including 'Fiction' and 'Non-Fiction'.Step 2: Loop and echo term names
The foreach loop prints each term's name followed by a comma and space, resulting in 'Fiction, Non-Fiction, '.Final Answer:
Fiction, Non-Fiction, -> Option CQuick Check:
get_terms() returns term objects with names [OK]
- Expecting taxonomy name instead of term names
- Trying to print array directly
- Forgetting to set 'hide_empty' to false to get all terms
wp_update_term(123, 'category', ['name' => 'New Name']);
Solution
Step 1: Check wp_update_term() parameters
The function takes term ID (integer), taxonomy slug (string), and an array of args to update.Step 2: Verify usage correctness
Using term ID 123, taxonomy 'category', and array with 'name' key is correct syntax and usage.Final Answer:
No error; this code correctly updates the term name -> Option BQuick Check:
wp_update_term() usage = correct [OK]
- Passing term ID as string instead of integer
- Using wrong parameter types
- Confusing taxonomy slug with term name
Solution
Step 1: Check if term exists in taxonomy
Useterm_exists('Adventure', 'book_genre')to verify if the term is already present.Step 2: Insert term only if it does not exist
If the term does not exist (negation), callwp_insert_term('Adventure', 'book_genre')to add it.Final Answer:
if (!term_exists('Adventure', 'book_genre')) { wp_insert_term('Adventure', 'book_genre'); } -> Option AQuick Check:
Check existence before insert = safe add [OK]
- Inserting term without checking existence
- Using term_exists() without taxonomy parameter
- Using wp_update_term() to add new term
Ready to prove it?
Pass the test and this pattern moves unknown
