Plugins are often unnecessary overhead for simple bulk database tasks. If you need to assign existing WordPress posts to a new category based on an existing tag, installing a bulk-edit plugin is overkill.
You can handle it directly with a short PHP script using WordPress core functions – then delete it once the task is complete.
This keeps your site lightweight and avoids adding permanent dependencies for one-time administrative work. Read on for how to do it quickly and easily with PHP. It’s a good idea to backup your database and website files before starting, just in case.
Identify the Category ID
First, you need the internal term ID of the category you want to assign posts to.
Go to:
Posts → Categories in your WordPress dashboard.
Click Edit on the category you want to use.
Look at the URL in your browser. You may see something like:
tag_ID=5- or
cat_ID=5
That number is the category term ID. Use it in the script below.
The PHP Script
Upload this file to your WordPress root directory (or adjust the path to wp-load.php if needed).
This script:
- Finds all posts with a given tag
- Assigns them to a category
- Preserves existing categories
- Uses only WordPress core functions
<?php
require_once('wp-load.php');
@set_time_limit(0);
// CONFIGURATION
$category_id = 5; // Target category ID - edit this
$tag_slug = 'ai'; // Source tag slug - edit this
$posts = get_posts([
'posts_per_page' => -1,
'fields' => 'ids',
'tax_query' => [
[
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $tag_slug,
]
],
]);
echo "Processing " . count($posts) . " posts...<br>";
foreach ($posts as $post_id) {
// TRUE preserves existing categories instead of replacing them
$result = wp_set_object_terms($post_id, (int)$category_id, 'category', true);
if (!is_wp_error($result)) {
echo "Post ID $post_id: Added to category.<br>";
} else {
echo "Post ID $post_id: Error updating.<br>";
}
}
echo "Done. Remove this file from your server immediately.";
?>
How to Run It
- Upload the PHP file to your server via FTP or your cPanel (web hosting) file manager
- Visit the file in your browser (e.g.
https://yoursite.com/blog/script-name.php) - Wait for it to finish processing
- Confirm the output shows the expected number of posts updated
- Delete the file immediately after use
Important Safety Notes
- Take a full database backup before running the script
- Do not leave this script on your server after execution
- Only run when logged in as an administrator or in a controlled environment
- On large sites, execution time may depend on server limits
Why This Works
WordPress already includes everything needed to manage taxonomy relationships efficiently. For one-time bulk updates like this, using wp_set_object_terms() is faster, cleaner, and avoids unnecessary plugins.
Keep your site lean – use code when it’s simpler than adding plugins. Plus, with this, there’s no third-party security risks possibly introduced.
If you want to move WordPress posts from one category to another category, without altering its other categories it might be associated with, check out the free script we provide in our post,
How to Safely Merge Categories in WordPress Using PHP (Free Script) – No plugins required!
Also related:
How to Automatically Share Your WordPress Blog Posts to Instagram
Do WordPress plugins sometimes leave stuff on your website after uninstalling the plugin?
Add SEO Meta Descriptions to Your WordPress Blog with UltimateWB – No Plugins Needed!
Looking for a website builder that has a customized and optimized WordPress blog option? Learn more about UltimateWB! We also offer web design packages if you would like your website designed and built for you.
Got a techy/website question? Whether it’s about UltimateWB or another website builder, web hosting, or other aspects of websites, just send in your question in the “Ask David!” form. We will email you when the answer is posted on the UltimateWB “Ask David!” section.
