Publish,Pending , draft category All Post Date randomize  plugin

To add the ability to randomize post dates for all posts in a specific category and add an option to the WordPress dashboard, you can extend your existing function and create an admin page where you can select the category. Here’s how you can do it:

Create a Custom Plugin (Recommended)

Creating a custom plugin is more modular and keeps your functionality separate from the theme. Here’s how to create a basic plugin for this feature.

2.1 Create a New Plugin

  1. Create a new folder for the plugin:
    • Go to your WordPress installation’s /wp-content/plugins/ directory.
    • Create a new folder called randomize-post-dates.
  2. Create a PHP file:
    • Inside this folder, create a file named randomize-post-dates.php.
  3. Add Plugin Header and Code:
    • Open the randomize-post-dates.php file and paste the following code:
<?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;
}
  1. Activate the Plugin:
    • Go to PluginsInstalled Plugins in your WordPress dashboard.
    • Activate your new plugin named “Randomize Post Dates by Category.”

Now you can use the plugin from the WordPress dashboard under ToolsRandomize Post Dates. Select the category, specify the date range, and randomize the post dates for all posts in that category.

 

Leave a Reply

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