Programmatically set and update Tags for WordPress Posts using PHP.

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

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 set and update WordPress Post Tags using PHP programmatically.

I decided to tackle this tutorial after receiving an inquiry about my creating WordPress Posts and Pages using a 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 is ensuring your web or local server’s timezone is set correctly.

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

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

Nevertheless, feel free to use Lynda.com to research videos and tutorials for any of the aforementioned considerations and prerequisites.

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 the 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.

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.

<?php

require_once("../wp-load.php");

?>

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

To keep this tutorial simple, please keep in mind that this tutorial discusses how to update a particular post and not multiple posts simultaneously.

If you want to update multiple posts simultaneously, I suggest reviewing get_posts() and query_post() built-in WordPress functions in conjunction with PHP loops.

Option 1: Setting and updating WordPress Post Tags using Tag IDs.

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

Next, set the artCatId variable equal to the tag id you want added to the post identified as the wpid variable.

Now comes the first built-in WordPress function to retrieve a specific post’s tag(s): 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 you’re 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 a zero-index array to return the updated tag ids of the respective post’s tags.

Do 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 post.

In addition, the third argument is not used in the 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 echo a “Yes…” line with its respective data, as shown in the code below. If not, then echo a “No…” line with its separate data.

<?php

require_once("../wp-load.php");

$wpid = 5949;

$artCatId = 21;

$postCat = wp_get_post_tags($wpid, array('fields' => 'ids'));

if(!in_array($artCatId,$postCat)){

	array_push($postCat,$artCatId);

	$event = wp_set_post_tags($wpid,$postCat);

}

if($event){

	echo 'YES - wpid: '.$wpid.' | tags: '.print_r($postCat).'<br><br>';

} else {

	echo 'No - wpid: '.$wpid.' | tags: '.print_r($postCat).'<br><br>'; 

}

?>

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 shown below.

<?php

require_once("../wp-load.php");

$wpid = 5949;

$artCatName = 'tag five';

$postCat = wp_get_post_tags($wpid, array('fields' => 'names')); 

if(!in_array($artCatName,$postCat)){

	array_push($postCat,$artCatName);

	$event = wp_set_post_tags($wpid,$postCat);

}

if($event){

	echo 'YES - wpid: '.$wpid.' | tags: '.print_r($postCat).'<br><br>';

} else {

	echo 'No - wpid: '.$wpid.' | tags: '.print_r($postCat).'<br><br>'; 

}

?>

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!

Share:
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.