Bird
0
0

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📝 Application Q15 of 15
Wordpress - Custom Post Types and Taxonomies
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?
Aif (!term_exists('Adventure', 'book_genre')) { wp_insert_term('Adventure', 'book_genre'); }
Bwp_insert_term('Adventure', 'book_genre');
Cif (term_exists('Adventure')) { wp_insert_term('Adventure', 'book_genre'); }
Dwp_update_term('Adventure', 'book_genre');
Step-by-Step Solution
Solution:
  1. Step 1: Check if term exists in taxonomy

    Use term_exists('Adventure', 'book_genre') to verify if the term is already present.
  2. 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.
  3. Final Answer:

    if (!term_exists('Adventure', 'book_genre')) { wp_insert_term('Adventure', 'book_genre'); } -> Option A
  4. Quick Check:

    Check existence before insert = safe add [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes