Created
February 20, 2020 20:50
-
-
Save mustardBees/40c6db0c90e016d2fdc4685ffa622eb8 to your computer and use it in GitHub Desktop.
Do you have a bunch of posts which share the same publish date & time? Do you find the order of your posts shifts around each time a query is run? This can happen after running a mass import of posts. Time Smudge spaces out the publish time randomly over the same/next day giving you a definite value for a buttery smooth, consistent sort order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Time Smudge | |
| Plugin URI: https://www.philwylie.co.uk/ | |
| Description: Do you have a bunch of posts which share the same publish date & time? Do you find the order of your posts shifts around each time a query is run? This can happen after running a mass import of posts. Time Smudge spaces out the publish time randomly over the same/next day giving you a definite value for a buttery smooth, consistent sort order. | |
| Version: 1.0.0 | |
| Author: Phil Wylie | |
| Author URI: https://www.philwylie.co.uk/ | |
| License: GPL2 | |
| */ | |
| /** | |
| * Class PW_Time_Smudge. | |
| */ | |
| class PW_Time_Smudge { | |
| /** | |
| * Current version number. | |
| */ | |
| const VERSION = '1.0.0'; | |
| /** | |
| * Post type. | |
| * | |
| * Change me to the desired post type. | |
| */ | |
| const POST_TYPE = 'product'; | |
| /** | |
| * Initialize the plugin by hooking into WordPress. | |
| */ | |
| public function __construct() { | |
| add_action( 'init', array( $this, 'time_smudge' ) ); | |
| } | |
| /** | |
| * Hit example.com/?time-smudge. Use at your own risk. Backup first! | |
| */ | |
| public function time_smudge() { | |
| if ( ! isset( $_GET['time-smudge'] ) ) { | |
| return; | |
| } | |
| $args = array( | |
| 'posts_per_page' => '-1', | |
| 'post_type' => SELF::POST_TYPE, | |
| 'no_found_rows' => true, | |
| 'cache_results' => false, | |
| ); | |
| $posts = new WP_Query( $args ); | |
| if ( $posts->have_posts() ) { | |
| $random_times = $this->random_unique_numbers( $posts->post_count ); | |
| $i = 0; | |
| while ( $posts->have_posts() ) { | |
| $posts->the_post(); | |
| $original_timestamp = get_the_date( 'U' ); | |
| $new_timestamp = $original_timestamp + $random_times[ $i ]; | |
| $new_formatted = date( 'Y-m-d H:i:s', $new_timestamp ); | |
| wp_update_post( | |
| array( | |
| 'ID' => get_the_ID(), | |
| 'post_date' => $new_formatted, | |
| 'post_date_gmt' => get_gmt_from_date( $new_formatted ) | |
| ) | |
| ); | |
| printf( "Post ID: %s - Original: %s / New: %s\n", get_the_ID(), $original_timestamp, $new_timestamp ); | |
| $i ++; | |
| } | |
| } | |
| echo 'Done.'; | |
| exit; | |
| } | |
| /** | |
| * Generate array of random unique numbers. | |
| * | |
| * @author Tony Yau | |
| * @link https://link.from.pw/3bXtQ2u | |
| */ | |
| public function random_unique_numbers( $count ) { | |
| $random_number_array = range( 0, 86400 ); | |
| shuffle( $random_number_array ); | |
| return array_slice( $random_number_array, 0, $count ); | |
| } | |
| } | |
| new PW_Time_Smudge(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment