
Auto-Clean and Schedule WordPress to Remove Tags from Old Posts (No Plugin Needed).
Quick Summary of Contents
The other day, I ran into an unusual situation while managing one of my many WordPress sites. I wanted a tag to automatically remove itself from a post after a certain number of days. No manual cleanup. No reminders. Just a quiet, behind-the-scenes cleanup.
Here’s an example: say you tag posts as “breaking news.” That label works for a while, but after two weeks, it’s not exactly breaking anymore. Instead of reminding yourself to go back and remove the tag, wouldn’t it be nice if WordPress could just handle it for you?
That’s what I wanted. Something lightweight. Simple. No plugin needed.
So, I wrote a short PHP script that could be scheduled to run automatically via a cPanel cron job. If that sounds intimidating, don’t worry—this walkthrough will help you set it up even if you’re not a developer.
What You’ll Need First:
- Access to your web hosting (via cPanel or FTP)
- A working WordPress site (you won’t touch the admin dashboard at all)
- A folder outside your WordPress install where we’ll keep the script (we’ll call it scripts)
- Comfort with copying and pasting some code—no deep coding knowledge needed
Step 1: Create the Script Folder and File
Log in to your site’s cPanel and go to File Manager, or use an FTP client. Find your WordPress installation, usually something like /public_html or /public_html/wordpress.
Inside that directory, create a new folder named scripts. Then, inside that, create a file called remove-breaking-news.php.
Now open that file with the built-in code editor (or any text editor) and paste in the following:
// Path to WordPress installation
$wp_load_path = __DIR__ . '/../wordpress/wp-load.php';
// Update this path based on your setup
require_once($wp_load_path);
// Load WordPress core
$tag_slug = 'breaking-news';
$args = [ 'post_type' => 'post', 'posts_per_page' => -1, 'tag' => $tag_slug, 'post_status' => 'publish', 'fields' => 'ids', ];
$posts = get_posts($args);
$now = current_time('timestamp');
foreach ($posts as $post_id) {
$post_date = get_post_time('U', true, $post_id);
// If the post is older than 14 days, remove the tag
if (($now - $post_date) >= 14 * DAY_IN_SECONDS) {
$tags = wp_get_post_tags($post_id, ['fields' => 'names']);
$updated_tags = array_filter($tags, function ($tag) use ($tag_slug) { return sanitize_title($tag) !== $tag_slug; });
wp_set_post_tags($post_id, $updated_tags, false);
error_log("Removed 'breaking news' tag from post ID $post_id");
}
}
What does this script do?
It’s simple. It finds all published posts tagged “breaking-news”, checks if the post is older than 14 days.
If yes, it removes the tag, and if no, it skips the post. Then, it logs any tag removals to your PHP error log.
Heads up: If your WordPress directory isn’t literally named WordPress, change the $wp_load_path line to match your actual folder name.
Step 2: Automate the Script with a Cron Job
Now that you’ve created the script, test it first. You can run it manually by navigating to its URL (e.g. http://yourdomain.com/scripts/remove-breaking-news.php). But I recommend testing with a dummy tag or category before you let it touch live posts.
Once you’ve confirmed it’s doing what it should, automate it.
Go back to your cPanel dashboard and search for “Cron Jobs.”
Scroll down to add a new one. Here’s an example of what to enter if you want the script to run every day at 3am:
0 3 * * *
In the Command to run field, enter something like this:
php /home/youruser/public_html/scripts/remove-breaking-news.php > /dev/null 2>&1
Adjust the path to match your actual server setup. You can find this path in your File Manager. If unsure, ask your hosting support—they’ll point you to the exact path.
A Few Extra Tips:
- Always test with a non-critical tag or category first.
- Double-check your cron job path—this is the most common error.
In addition, if you want better logging, you can tweak the script to write to a file or even send email alerts. For now, it logs only to your server’s error log by default.
That’s it. You now have a custom, automatic way to remove old tags from WordPress posts without relying on plugins.
Let me know if you hit any snags. Happy coding!
Leave a Comment