Auto-Tag 800+ posts with “Auto Tags” – automatically

The problem

How to auto-tag your posts with the plugin “Auto Tags” after they’ve been published already without manually edit and save each post to get them auto-tagged? Additionally, you don’t have SSH access to your server and the PHP max_execution_time is limited to 30 or 60 seconds so a script’s runtime would exceed and result in a blank page or fatal error?

The solution

Put a file auto-tag.php (or whatever you’d like) in your WP document root and fill it with the following contents:

<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', '1');

define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
global $wpdb;

if( !isset($_GET['offset']) ) {
  $offset = (int)$_GET['offset'];
}

$all_post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'post'");
$post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'post' ORDER BY ID ASC LIMIT " . $_GET['offset'] . ",3");

if( $_GET['offset'] >= count($all_post_ids) ) {
  die('Done.');
  exit;
}

foreach( $post_ids as $post_id ) {
  do_action( 'wp_insert_post', $post_id );
}
$offset = $_GET['offset']+3;
echo '<meta http-equiv="refresh" content="2; url=' . $_SERVER['SCRIPT_NAME'] . '?offset=' . $offset . '">';
?>

What does it do?

To avoid the script being aborted due to the PHP max_execution_time, the above script auto-tags 3 posts at a time, then automatically reloads the page, auto-tags the next 3 posts and so on. All you have to do is watch the script do the work for you, and get a coffee.

Requirements

Tags: , , ,

Leave a comment