Loop Through All Posts and Update

Here’s a quick script that we used to loop through all posts and update them one by one with dynamic data (well… kinda dynamic)…

<?php

require_once __DIR__ . '/wp-load.php';

// Get the global $wpdb object
global $wpdb;

// Query all posts
$posts = $wpdb->get_results("SELECT ID, post_date FROM {$wpdb->posts}");

// Loop through each post and update the post_modified column
foreach ($posts as $post) {
    $wpdb->update(
        $wpdb->posts,
        ['post_modified' => $post->post_date, 'post_modified_gmt' => $post->post_date],
        ['ID' => $post->ID]
    );
}

// Output a success message
echo "All posts have been updated successfully.\n";

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *