admin管理员组

文章数量:1415491

My registered custom taxonomy name is ov-category.. There is already existing a parent term called Gender now i want to add a child called Male :

$parent_term = term_exists( 'Gender', 'ov-category' ); 
$parent_term_id = $parent_term['term_id']; // get numeric term id

echo $parent_term_id; // shows the correct parent ID, that means term_exists() does work!!

// Inserting the child term 'Male'
wp_insert_term(
    'Male', // the term 
    'ov-category', // the taxonomy
     array(
    'description'=> '',
    'slug' => '',
    'parent'=> $parent_term_id
    )
 );

even if i try to insert a parent-only term it doesnt work. but i can read their correct IDs using term_exists() and those are correct because i checked them in the database. By the way: i added Gender over the UI. I need a way that those terms are automatically added when my plug-in is installed.

My registered custom taxonomy name is ov-category.. There is already existing a parent term called Gender now i want to add a child called Male :

$parent_term = term_exists( 'Gender', 'ov-category' ); 
$parent_term_id = $parent_term['term_id']; // get numeric term id

echo $parent_term_id; // shows the correct parent ID, that means term_exists() does work!!

// Inserting the child term 'Male'
wp_insert_term(
    'Male', // the term 
    'ov-category', // the taxonomy
     array(
    'description'=> '',
    'slug' => '',
    'parent'=> $parent_term_id
    )
 );

even if i try to insert a parent-only term it doesnt work. but i can read their correct IDs using term_exists() and those are correct because i checked them in the database. By the way: i added Gender over the UI. I need a way that those terms are automatically added when my plug-in is installed.

Share Improve this question edited Aug 16, 2017 at 4:27 Berat Çakır asked Aug 15, 2017 at 23:34 Berat ÇakırBerat Çakır 91 silver badge3 bronze badges 5
  • Put a check for if the term_exists() function returns an array (the term was found), or false. See the docs in the Codex for an example. codex.wordpress/Function_Reference/term_exists . It could be that your term-exists() function is returning false, so the rest of the code is not working. – Rick Hellewell Commented Aug 16, 2017 at 2:50
  • Please read my post before you write. i said that term_exists() is working and im getting the correct id.. thank you – Berat Çakır Commented Aug 16, 2017 at 4:18
  • is wp_insert_term returning a WP_Error object? – inarilo Commented Aug 16, 2017 at 4:43
  • assign to a variable $ret=wp_insert_term(...) and use codex.wordpress/Function_Reference/is_wp_error on it – inarilo Commented Aug 16, 2017 at 4:57
  • Where is this code? You mention plugin installation. An important difference to note is that term_exists does not validate the taxonomy, where wp_insert_term checks if it's a registered taxonomy. term_exists will succeed even if the taxonomy isn't registered in the current request, where wp_insert_term will fail. – Milo Commented Aug 16, 2017 at 5:47
Add a comment  | 

1 Answer 1

Reset to default 1

Thanks to Milo who asked an important question: Where is this code?

I put it below where ive have registered the taxonomy and then it worked:

function register_taxonomy() {
       $labels = array(...);
       $args = array(...);

       register_taxonomy( 'ncategory', null, $args );

        $parent_term = term_exists( 'Gender', 'ncategory' ); // array is returned if taxonomy is given
        $parent_term_id = $parent_term['term_id']; // get numeric term id

//echo $parent_term_id;

  $ret = wp_insert_term(
 'Male',
  'ncategory',
     array(
        'description'=> '',
    'slug' => '',
    'parent'=> $parent_term_id
  )
);

   echo is_wp_error($ret);

}

本文标签: pluginswpinsertterm() doesnt insert a term