wp_insert_term is adding a term that has no name

I’m trying to insert a term for a custom taxonomy and a custom post type in WordPress. The problem I’m running in to is that it successfully inserts a term, and at the right hierarchy level, but the term has no name. The line that inserts the term is:

$lasttermarray = wp_insert_term($categoryname, "department", array( "parent" => $parentcategory ) );

I’ve checked to make sure that both $categoryname and $parentcategory are set to the right values. Like I said, the term is being inserted, just with a blank name. The slug of the blank term is being set to the ID of the new term.

Read More

Is there any reason why the function would not be reading the name variable and causing it to do this? Thanks for your help.

Related posts

1 comment

  1. As noted in the OP’s comments, some non-ASCII characters may not be supported in term names under certain conditions. To replace all suspect characters with an underscore, use the following code:

    $categoryname = preg_replace('/[^a-z0-9]/i', '_', strtolower($categoryname));
    wp_insert_term($categoryname, "department");
    

    To replace all suspect characters with a dash, use the following code:

    $categoryname = preg_replace('/[^a-z0-9]/i', '-', strtolower($categoryname));
    wp_insert_term($categoryname, "department");
    

    To simply remove all suspect characters, use the following code:

    $categoryname = preg_replace('/[^a-z0-9]/i', '', strtolower($categoryname));
    wp_insert_term($categoryname, "department");
    

Comments are closed.