Wordpress custom taxonomy - wp_set_object_terms creates taxonomy duplicates?

61 Views Asked by At

We are building a small plugin for adding custom posts to our website. These posts have 'attributes' like categories. These posts come from an external feed. When adding a new posts we want to add a taxonomy (when not exists) and add terms to that for that specific post. We add it like this (example of 1 post):

$postID = 2312;
$terms = array("foo", "bar", "hello", "world");
$taxonomy = "DrivingLicense";

wp_set_object_terms($postID, $terms, $taxonomy);

After this, the wp_term_taxonomy table has 4 entries:

term_taxonomy_id    term_id taxonomy        parent      count
2649            2649    DrivingLicense      0           1
2650            2650    DrivingLicense      0           1
2651            2651    DrivingLicense      0           1
2652            2652    DrivingLicense      0           1

What am I doing wrong here?

We expect that we get only one taxonomy "DrivingLicense", with unique terms "foo", "bar", "hello", "world" linked by the wp_term_relationships table.

1

There are 1 best solutions below

1
Oshrib On

It looks like your approach with wp_set_object_terms() is almost there, but there's a bit of a mix-up in how WordPress handles taxonomies and terms. Let's clear this up and get your plugin running smoothly!

Understanding WordPress Taxonomies and Terms Taxonomy: This is a grouping mechanism, like 'category' or 'tag'. In your case, "DrivingLicense" is the taxonomy.

Terms: These are items within a taxonomy. For example, "foo", "bar", "hello", and "world" are terms in the "DrivingLicense" taxonomy.

The Issue with Your Current Approach When you use wp_set_object_terms(), WordPress expects the terms to already exist in the taxonomy. If they don't, WordPress creates new terms. However, it seems like WordPress is treating each of your terms as both a new term and a new taxonomy, hence the multiple entries in the wp_term_taxonomy table.

How to Fix It Ensure Taxonomy Exists: Before adding terms, make sure your custom taxonomy "DrivingLicense" is registered with register_taxonomy(). This should be done in your plugin's setup phase, typically hooked to init.

Check and Add Terms: Before associating terms with the post, check if they exist. If not, add them. Use term_exists() to check and wp_insert_term() to add new terms.

Associate Terms with Post: Once the terms are assured to exist, use wp_set_object_terms() to associate them with your post.

// Register taxonomy (usually in an init hook)
function register_my_taxonomy() {
    register_taxonomy('DrivingLicense', 'post', /* Other args */);
}
add_action('init', 'register_my_taxonomy');

// Function to add terms to a post
function add_terms_to_post($postID, $terms, $taxonomy) {
    foreach ($terms as $term) {
        // Check if the term exists
        if (!term_exists($term, $taxonomy)) {
            // Insert the term if it doesn't exist
            wp_insert_term($term, $taxonomy);
        }
    }
    // Now associate terms with the post
    wp_set_object_terms($postID, $terms, $taxonomy);
}

// Usage
$postID = 2312;
$terms = ["foo", "bar", "hello", "world"];
$taxonomy = "DrivingLicense";
add_terms_to_post($postID, $terms, $taxonomy);