I added some Tags programmatically with wp_insert_term(), and later I call get_tags() to use them, but it returns an empty array, although they are inserted in the database.

Here is my code:


$tags = array(
    array('name' => 'Beachfront Escapes', 'slug' => 'beachfront-escapes'),
    array('name' => 'Group Holidays', 'slug' => 'group-holidays'),
    array('name' => 'City Breaks', 'slug' => 'city-breaks')

foreach ($tags as $tag) {
    if (!term_exists($tag['name'], 'post_tag')) {
        wp_insert_term($tag['name'], 'post_tag', array('slug' => $tag['slug']));


$tags = get_tags();
var_dump($tags); // empty array()

Am I doing something wrong?


Thanks to @Milo I could fix it. The problem was, that the tags were not assigned to any post.


$tags = get_tags(array('hide_empty' => false));

The hide_empty param do the trick.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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