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.

I'm a total newbie to Wordpress, however I've been asked to develop a PHP script to import tags from an Excel spreadsheet to the WP db. So far, so good...

BUT: there's this strange behavior I need you to help me with. I upload the tags fine (in batches of 2-300 a time), I verify they have been added by going into the WP-Admin and checking if they're all there. I then add a tag to a post and go the post's page and click on the tag I just added and I get a Error 404. If I click on edit tag, go into the edit page and simply click Update without changing anything, everything then works fine.

Could you please suggest what I should check?

Edit: I'm adding a bit more info on how my script works:

First I create an array from the existing tags so I can add the new tags and update the db:

// Get array and unserialize it so we can add data to it    
$query_tags = "SELECT option_id, option_value FROM options where option_name = 'wpseo_taxonomy_meta'";
$tags = mysql_query($query_tags) or die(mysql_error());
$row_tags = mysql_fetch_assoc($tags);
$totalRows_tags = mysql_num_rows($tags);

// unserialize data and create the multidimensional array $md_array
    $md_array = unserialize($row_tags['option_value']);

Now that the array has been populated, I begin adding new tags from the XLS.The XLS has the following columns: Name, Slug, Description, SEO Title, SEO Description, Breadcrumbs Title. I loop through the XLS adding to the terms table:

sprintf("INSERT INTO ".$dbprefix."terms (name , slug, term_group) VALUES (%s, %s, %s)",
       GetSQLValueString($val[0], "text"),
       GetSQLValueString($val[1], "text"), // or $slug = createslug($val[1]);
       GetSQLValueString(0, "int"));

Then, I add to table term_taxonomy:

sprintf("INSERT INTO ".$dbprefix."term_taxonomy (term_id, taxonomy, description, parent, count) VALUES (%s, %s, %s, %s, %s)",
    GetSQLValueString($id, "int"),
    GetSQLValueString('post_tag', "text"),
    GetSQLValueString($val[2], "text"),
    GetSQLValueString(0, "int"),
    GetSQLValueString(0, "int"));

During the loop I also add new elements to the array I created in the beginning

$newtag = array (
    'wpseo_title' => $val[3],
    'wpseo_desc' => $val[4],
    'wpseo_bctitle' => $val[5],
    'wpseo_noindex' => 'index',
    'wpseo_sitemap_include' => 'always');   

$md_array["post_tag"][$id] = $newtag;

And finally:

$newdata = serialize($md_array);
$query_data = "update options set option_value ='".$newdata."' where option_name = 'wpseo_taxonomy_meta'";

If you have any questions about my code please ask. Thanks!

share|improve this question
Have you updated your permalinks? Is there any template that will handle the job of a taxonomy page? I would have probably used a WP function to import the tags: codex.wordpress.org/Function_Reference/wp_insert_term –  GhostToast Jun 4 '13 at 13:45
add comment

Your Answer


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

Browse other questions tagged or ask your own question.