Subscribe to RSS

How To Create Tags For WordPress Posts

How To Create Tags For WordPress Posts

Welcome to another quick article in the WordPress Development Tips Series.

During the development of my plugins I discover quite a few tips and tricks along the way. In this series I try to share the most important or the ones I feel lack the most documentation.

This tip involves how to create a tag via PHP and assign it to a post.

The code is quite simple, and uses just a single WordPress function, already included in the WordPress code.

The function to use is named wp_set_object_terms, and the usage is as follows:

wp_set_object_terms( $postid, $newtagarray, ‘post_tag’, true );

Example:

wp_set_object_terms( 1, array(‘my new tag’), ‘post_tag’, true );

The code in the example will create (if it doesn’t already exist) a tag named “my new tag”, and assign it to post 1.

wp set object terms How To Create Tags For WordPress Posts

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

2 Comments

  1. Hey, that’s a great tip – thanks! This is my first WordPress site but I’m still a touch lost… there’s just so many plugins, themes, and options! But I’m slowly getting the hang of it, thankfully. I guess it shouldn’t take this long to setup next time either. Anyhow, cool blog – I’m subscribed to your feed now so I’ll be checking in regularly!

  2. Hey – thanks for the tip! The site I have here is my first one based off on WordPress.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

PRIVACY POLICY: Your privacy is important to us. We will never sell or rent your email address and you can unsubscribe at any time.