Publishd Post bulk date randomizer

      No Comments on Publishd Post bulk date randomizer

To create a post date randomizer for bulk updating post dates in WordPress, including randomizing the year for selected posts, you can use a custom PHP script. Below is an example of a function that will allow you to select a bulk set of posts by their IDs and change the year of the post date randomly:

<?php

// Add a custom bulk action to the post list area
function add_randomize_date_bulk_action($bulk_actions) {
    $bulk_actions['randomize_post_dates'] = __('Randomize Post Dates', 'textdomain');
    return $bulk_actions;
}
add_filter('bulk_actions-edit-post', 'add_randomize_date_bulk_action');

// Handle the bulk action to randomize post dates
function handle_randomize_date_bulk_action($redirect_url, $doaction, $post_ids) {
    if ($doaction !== 'randomize_post_dates') {
        return $redirect_url;
    }

    // Call the randomize function for the selected post IDs
    randomize_post_dates($post_ids);

    // Add a custom query parameter to show the result of the bulk action
    $redirect_url = add_query_arg('randomized', count($post_ids), $redirect_url);
    return $redirect_url;
}
add_filter('handle_bulk_actions-edit-post', 'handle_randomize_date_bulk_action', 10, 3);

// Show admin notice after randomization
function randomize_date_bulk_admin_notice() {
    if (!empty($_REQUEST['randomized'])) {
        $count = intval($_REQUEST['randomized']);
        printf('<div id="message" class="updated notice is-dismissible"><p>' . 
               _n('%s post date has been randomized.', '%s post dates have been randomized.', $count, 'textdomain') . 
               '</p></div>', $count);
    }
}
add_action('admin_notices', 'randomize_date_bulk_admin_notice');

// Function to randomize post dates for all post statuses (drafts and published)
function randomize_post_dates($post_ids = [], $start_year = 2014, $end_year = 2022) {
    if (empty($post_ids) || !is_array($post_ids)) {
        return false;
    }

    foreach ($post_ids as $post_id) {
        // Get the current post
        $post = get_post($post_id);

        // If the post date is '0000-00-00 00:00:00' (common for drafts), use the current time
        if ($post->post_date == '0000-00-00 00:00:00') {
            $post_date = current_time('mysql');
        } else {
            $post_date = $post->post_date;
        }

        // Extract the current month, day, and time
        $month_day_time = date('m-d H:i:s', strtotime($post_date));

        // Generate a random year between the start and end year
        $random_year = rand($start_year, $end_year);

        // Create the new post date with the random year
        $new_post_date = $random_year . '-' . $month_day_time;

        // Make sure the date is formatted correctly
        $new_post_date_gmt = get_gmt_from_date($new_post_date);

        // Update the post date in the database
        wp_update_post([
            'ID'           => $post_id,
            'post_date'    => $new_post_date,
            'post_date_gmt'=> $new_post_date_gmt,
            'edit_date'    => true, // Ensure the date is updated even for drafts
        ]);
    }

    return true;
}

How it work:

  • Go to Posts in the WordPress admin area.
  • Select multiple posts using the checkboxes.
  • In the “Bulk actions” dropdown, select Randomize Post Dates and click Apply.
  • The post dates will be randomized for the selected posts.

The randomization of post dates works for all post statuses (including drafts)

How to setup:

Its so easy just add the code to function.php

Year Range: The random year range is now between 2014 and 2022 ($start_year = 2014 and $end_year = 2022).

Leave a Reply

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