📖 Scenario: You are building a WordPress plugin that manages custom taxonomy terms for a blog. You want to create, configure, and display taxonomy terms programmatically.
🎯 Goal: Build a simple WordPress plugin that registers a custom taxonomy, adds terms to it, and displays those terms on a page.
📋 What You'll Learn
Register a custom taxonomy called genre for posts
Add three terms: Fiction, Non-Fiction, and Biography
Retrieve and display all terms of the genre taxonomy
💡 Why This Matters
🌍 Real World
Managing custom categories or tags for posts helps organize content clearly for readers and site admins.
💼 Career
WordPress developers often need to create and manage taxonomies to customize content classification and improve site navigation.
Progress0 / 4 steps
1
Register the custom taxonomy genre
Write code to register a custom taxonomy called genre for the post post type using register_taxonomy. Use the labels 'Genres' for plural and 'Genre' for singular. Place this code inside a function called register_genre_taxonomy and hook it to init action.
Wordpress
Hint
Use register_taxonomy inside a function hooked to init.
2
Add terms Fiction, Non-Fiction, and Biography
Create a function called add_genre_terms that adds the terms Fiction, Non-Fiction, and Biography to the genre taxonomy using wp_insert_term. Hook this function to init with priority 20.
Wordpress
Hint
Use wp_insert_term inside a function hooked to init with priority 20.
3
Retrieve all terms from genre taxonomy
Write a function called get_all_genre_terms that uses get_terms to retrieve all terms from the genre taxonomy. Store the result in a variable called $terms.
Wordpress
Hint
Use get_terms with taxonomy set to genre and hide_empty set to false.
4
Display all genre terms on a page
Create a shortcode function called display_genre_terms that calls get_all_genre_terms, loops through the terms, and returns an unordered list (<ul>) of term names. Register this shortcode with the tag show_genres.
Wordpress
Hint
Use a shortcode function that returns an unordered list of term names and register it with add_shortcode.
Practice
(1/5)
1. What is the primary purpose of taxonomy terms in WordPress?
easy
A. To handle database backups automatically
B. To create user accounts and manage permissions
C. To store theme settings and customization options
D. To group and organize content for easier filtering and display
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 D
Quick Check:
Taxonomy terms = content grouping [OK]
Hint: Taxonomy terms group content, not users or settings [OK]
Common Mistakes:
Confusing taxonomy terms with user roles
Thinking taxonomy terms manage theme options
Assuming taxonomy terms handle backups
2. Which of the following is the correct way to insert a new taxonomy term in WordPress?
easy
A. wp_insert_term('Term Name', 'taxonomy_name');
B. insert_term('Term Name', 'taxonomy_name');
C. add_term('Term Name', 'taxonomy_name');
D. create_term('Term Name', 'taxonomy_name');
Solution
Step 1: Identify the correct function name
The official WordPress function to add a taxonomy term is wp_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 A
Quick Check:
Insert term function = wp_insert_term() [OK]
Hint: Remember the prefix 'wp_' for WordPress core functions [OK]
Common Mistakes:
Using non-existent functions like insert_term
Omitting the 'wp_' prefix
Confusing with functions for posts or users
3. What will the following code output if the taxonomy 'genre' has terms 'Fiction' and 'Non-Fiction'?
A. The taxonomy slug 'category' is incorrect for terms
B. No error; this code correctly updates the term name
C. Term ID must be a string, not an integer
D. The third parameter should be a string, not an array
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 B
Quick Check:
wp_update_term() usage = correct [OK]
Hint: wp_update_term() needs term ID, taxonomy, and args array [OK]
Common Mistakes:
Passing term ID as string instead of integer
Using wrong parameter types
Confusing taxonomy slug with term name
5. You want to add a new term 'Adventure' to the custom taxonomy 'book_genre' only if it does not already exist. Which code snippet correctly achieves this?
hard
A. if (!term_exists('Adventure', 'book_genre')) { wp_insert_term('Adventure', 'book_genre'); }
B. wp_insert_term('Adventure', 'book_genre');
C. if (term_exists('Adventure')) { wp_insert_term('Adventure', 'book_genre'); }
D. wp_update_term('Adventure', 'book_genre');
Solution
Step 1: Check if term exists in taxonomy
Use term_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), call wp_insert_term('Adventure', 'book_genre') to add it.
Final Answer:
if (!term_exists('Adventure', 'book_genre')) { wp_insert_term('Adventure', 'book_genre'); } -> Option A
Quick Check:
Check existence before insert = safe add [OK]
Hint: Use term_exists() before wp_insert_term() to avoid duplicates [OK]
Common Mistakes:
Inserting term without checking existence
Using term_exists() without taxonomy parameter
Using wp_update_term() to add new term
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.