Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Here's essentially what I'm doing :

create_taxonomy_record(array(
            'Label',
            'tax_parent_slug',
            'Label Related',
            'this_tax_term_slug'));
/* ---> Create a child */
            create_taxonomy_record(array(
            'Label 2',
            'this_tax_term_slug',
            'Label Related',
            'this_tax_term_slug_a_child'));

and here's that function named create_taxonomy_record (its very vanilla - echos are for debug purposes)

create_taxonomy_record($args)
        {

            $term = term_exists($args[0], $args[1]);
            if ($term == 0 || $term == null) {

                wp_insert_term(
                    $args[0], // the term
                    $args[1], // the taxonomy
                    array(
                        'description'=> $args[2],
                        'slug' => $args[3],
                        'parent'=> $args[1]
                        )
                );
            }
            else { echo "<h1>" . $args[0] . " exists in parent " . $args[1] ."</h1>";}
        }

So this isn't working but its also not hard failing. I have warnings / errors on and this code provides neither.

What's Working:

  • All 1st tier terms in the taxonomy are registering fine(1->n, i've run tests against 10k calls to this function and all perform nominally)

What's Not Working:

  • when I try to assign a child term to a parent term. I do not get a warning, failure or false match of existence, i just don't get anything.

Now I did find this :

Inserting terms in an Hierarchical Taxonomy

However this did not seem to fix the issue. Or rather perhaps I am going about the cache clear incorrectly.

What I tried for the cache clear looked something like this:

create_taxonomy_record(array(
                'Label',
                'tax_parent_slug',
                'Label Related',
                'this_tax_term_slug'));
    /* ---> Create a child */

/* Tried it here */
delete_option("this_tax_term_slug_children");
                create_taxonomy_record(array(
                'Label 2',
                'this_tax_term_slug',
                'Label Related',
                'this_tax_term_slug_a_child'));

/* and here - all permutations/ combinations should be considered attempted */
delete_option("this_tax_term_slug_children");

No dice.

share|improve this question
    
Is the second code snippet a user function definition? If so, you left the word function off the beginning. –  Charles Clarkson Oct 10 '13 at 19:45
    
hey charles no - its the same call to the method. Delete_option is (from my understanding?) a WP function, if thats what you are asking –  Brandt Solovij Oct 10 '13 at 19:54
add comment

1 Answer

up vote 0 down vote accepted

It doesn't work because you use wp_insert_term function incorrectly. Please read the codex manual page carefully and you will see that the second argument is not parent slug, but your custom taxonomy.

So your function should look like this:

create_taxonomy_record($args) {
    $parent_term = 0;
    if (!empty($args[2]) && ($parent_term = term_exists($args[2], $args[1])) {
       $parent_term = $parent_term['term_id'];
    }

    $term = term_exists($args[0], $args[1]);
    if ($term == 0 || $term == null) {
        wp_insert_term(
            $args[0], // the term
            $args[1], // the taxonomy
            array(
                'parent'      => $parent_term,
                'description' => $args[3],
                'slug'        => $args[4],
            )
        );
    }

    else { echo "<h1>" . $args[0] . " exists in parent " . $args[1] ."</h1>";}
}

Pay attention to $args array, now it should has 5 elements:

  1. Term
  2. Taxonomy
  3. Parent term slug
  4. Description
  5. Term slug

And you can use it like this:

create_taxonomy_record(array(
    'Label',
    'your_custom_taxonomy_name_has_to_be_here',
    0,
    'Label Related',
    'this_tax_term_slug',
));

/* ---> Create a child */

create_taxonomy_record(array(
    'Label 2',
    'your_custom_taxonomy_name_has_to_be_here',
    'this_tax_term_slug',
    'Label Related',
    'this_tax_term_slug_a_child'
));
share|improve this answer
    
Bingo. Thanks Eugene. The nomenclature on the WP Codex leaves one wanting sometimes. Much appreciated. –  Brandt Solovij Oct 10 '13 at 20:22
    
you are welcome! –  Eugene Manuilov Oct 10 '13 at 20:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.