André Amorim

Crafting Web Experiences

Home » My Notes » Randomize all WP post dates

//

Randomize all WP post dates

<?php
/**
 * Randomize published and modified dates for all posts.
 *
 * HOW TO USE:
 * 1. Add this snippet to your functions.php (or run it via a plugin like Code Snippets).
 * 2. Visit: yoursite.com/?randomize_post_dates=1&start_year=2019&end_year=2023&post_type=post
 * 3. REMOVE the snippet immediately after running it.
 */

function randomize_all_post_dates() {
    if ( ! isset( $_GET['randomize_post_dates'] ) || $_GET['randomize_post_dates'] !== '1' ) {
        return;
    }

    if ( ! current_user_can( 'manage_options' ) ) {
        echo 'Unauthorized.';
        exit;
    }

    $start_year = isset( $_GET['start_year'] ) ? intval( $_GET['start_year'] ) : null;
    $end_year   = isset( $_GET['end_year'] )   ? intval( $_GET['end_year'] )   : null;
    $post_type  = isset( $_GET['post_type'] )  ? sanitize_key( $_GET['post_type'] ) : null;

    if ( ! $start_year || ! $end_year ) {
        echo 'Error: Please provide both start_year and end_year as URL parameters.';
        exit;
    }

    if ( ! $post_type ) {
        echo 'Error: Please provide a post_type as a URL parameter.';
        exit;
    }

    if ( ! post_type_exists( $post_type ) ) {
        echo "Error: Post type '{$post_type}' does not exist.";
        exit;
    }

    if ( $start_year > $end_year ) {
        echo 'Error: start_year cannot be greater than end_year.';
        exit;
    }

    $current_year = intval( date( 'Y' ) );
    if ( $start_year < 2000 || $end_year > $current_year ) {
        echo "Error: Years must be between 2000 and {$current_year}.";
        exit;
    }

    global $wpdb;

    $posts = get_posts( [
        'post_type'      => $post_type,
        'post_status'    => 'publish',
        'posts_per_page' => -1,
        'fields'         => 'ids',
    ] );

    if ( empty( $posts ) ) {
        echo "No posts found for post type '{$post_type}'.";
        exit;
    }

    $start_timestamp = mktime( 0, 0, 0, 1, 1, $start_year );
    $end_timestamp   = mktime( 23, 59, 59, 12, 31, $end_year );

    $count = 0;

    foreach ( $posts as $post_id ) {
        $random_published   = rand( $start_timestamp, $end_timestamp );
        $date_published     = date( 'Y-m-d H:i:s', $random_published );
        $date_published_gmt = get_gmt_from_date( $date_published );

        $random_modified   = rand( $random_published, $end_timestamp );
        $date_modified     = date( 'Y-m-d H:i:s', $random_modified );
        $date_modified_gmt = get_gmt_from_date( $date_modified );

        $wpdb->update(
            $wpdb->posts,
            [
                'post_date'         => $date_published,
                'post_date_gmt'     => $date_published_gmt,
                'post_modified'     => $date_modified,
                'post_modified_gmt' => $date_modified_gmt,
            ],
            [ 'ID' => $post_id ],
            [ '%s', '%s', '%s', '%s' ],
            [ '%d' ]
        );

        clean_post_cache( $post_id );
        $count++;
    }

    echo "Done! Dates randomized for {$count} '{$post_type}' posts between {$start_year} and {$end_year}. <strong>Now remove this snippet.</strong>";
    exit;
}
add_action( 'template_redirect', 'randomize_all_post_dates' );

Published date:

Modified date: