Programmatically set and update Tags for WordPress Posts using PHP

“It’s been a long time, I shouldn’t of left you, Without a dope tutorial to go too…”

Well, let’s just say I’m channeling a bit of Timbaland and the late Aaliyah’s “Try Again” today to bring your another tutorial.

Indeed, it’s been nearly two months since I last graced you with a tutorial.

Although I’ve spent the last few months covering all things emoji domains with more soon to come, today’s tutorial covers how to programmatically set and update WordPress Post Tags using PHP.

I decided to tackle this tutorial after receiving an inquiry on my creating WordPress Posts and Pages using PHP video tutorial.

We don’t have much time, so let’s get started.

Tutorial Pre-Requisites

Today’s tutorial requires that the following pre-requisites are met:

Another important consideration to note is ensuring that your web or local server’s timezone is set correctly.

The timezone setting can be modified via the php.ini, httpd.conf or the appropriate system file containing timezone settings.

In short, the timezone setting greatly impacts when a post is scheduled and published.

Nevertheless, feel free to use to research videos and tutorials for any of the aforementioned considerations and pre-requisites.

Accessing built-in WordPress native functionality

Today’s tutorial makes use of the following built-in WordPress functions to set and update WordPress Post Tags:

But to access the aforementioned functions, ensure that your PHP file you’ll soon create is within the WordPress website directory.

Feel free to create a folder named migration within the WordPress website directory to place your PHP file in.

For this tutorial, I created a folder named migration within my locally-hosted WordPress environment host in the ski web directory, which lives in my XAMPP htdocs directory.

Next, create your PHP file, saving and naming it update-post-tag.php.

Once your file has been created, be sure to require the wp-load.php file.

Including this file offers access to using built-in WordPress functions. As stated earlier, this tutorial uses wp_get_post_tags() and wp_set_post_tags() functions.

Next, we’ll cover two different methods to use for setting and updating WordPress Post Tags using PHP.

To keep this tutorial simple in nature, please keep in mind that this tutorial discusses how to update a singular post and not multiple posts at one time.

If you would like to update multiple posts at a time, then I suggest reviewing get_posts() and query_post() built-in WordPress functions in conjunction with PHP loops.

Option 1: Setting and update WordPress Post Tags using Tag IDs

First, set the wpid variable of the post you would like to update.

Next, set the artCatId variable equal to the tag id you would like to be add to the post identified as the wpid variable.

Now comes the first built-in WordPress function to retrieve the the tag(s) for a specific post: wp_get_post_tags.

Create the postCat variable and set it equal to the wp_get_post_tags.

The wp_get_post_tags accepts the post id as the first argument, and we’ll use an associate array to return the ids of the respective post’s tags (as shown below).

Next, define an if statement that checks whether or not the tag id your requesting to be added to a specific post already exists as a value within the postCat variable’s array value. The if statement makes use of the built-in PHP in_array function.

If the value defined as the artCatId is not found in the array value of the postCat variable value, then the new tag id is pushed as a new value to the postCat variable array.

The last step within the if statement is to set the new tag value for the respective post using the wp_set_post_tags function.

Create the event variable and set it equal to the wp_set_post_tags.

The wp_set_post_tags accepts the post id as the first argument, and we’ll use an zero-index array to return the updated tag ids of the respective post’s tags.

Do take time to read up on the wp_set_post_tags function as it provides a third argument that allows for updating and overwriting tag ids for a respective post.

In addition, the third argument is not used in tutorial (as explained in the video tutorial), but feel free to use it as you see fit.

After closing the if statement, the final step is echoing whether or not the post tags were successfully updated using an if statement based on the event variable.

If the event variable contains a value, we simply echo a “Yes…” line with it’s respective data as shown in the code below. If not, then simply echo a “No…” line with it’s respective data.

Option 2: Setting and updating WordPress Post Tags using Tag Names

The second option for setting and updating WordPress Post Tags using Tag names uses the very same codebase, yet it modifies a few variables and variable values as show below.

Time to save and test your code

Well, it’s now time to test your technical prowess to see if you can successfully update a WordPress post’s tag(s).

Be sure to save your code, whichever option you choose to use, and open a web browser to execute the code.

Hopefully, your web browser displays a “Yes…” line of data instead of a “No…”.

If you need additional help, then please don’t hesitate to leave me a comment or question below.

Also, feel free to watch the quick and easy video tutorial where I explain both options for setting and updating WordPress Post Tags using PHP.

I have a few more tutorials I’ll bring you to in the coming weeks, so stay tuned. Enjoy and that’s all for now!

Written by Alvin Brown
He's an experienced and passionate serial entrepreneur, founder and publisher of Kickstart Commerce. Alvin possesses a great love for startups dominating their market using profitable digital strategies for greater commerce.