Randomize a sort every two hours, or a custom defined interval

Creating randomize sort never been easier, custom intervals can be set to whatever time-frame ins need. The sort will random the posts or any custom post types order.
First a sort is required to be created for a specific area, the Manual Sort tab need to be set. For additional details on how to create a sort see Create simple sort for a Custom Post Type


The following step will be to identify the sort_view_id which held the current list and need randomized. This can be found on page url after Manual Order tab click or hover on it. As alternative it can be found within the page html when search for instance ‘var sort_view_id’.


The following code should be placed within your theme functions.php or within a custom plugin.

First define the custom interval when the randomize will trigger, in this case every two hours. If need to be set for hourly, twice daily, daily, no need to create a new entry as thy are available already through WordPress see wp get schedules

     add_filter( 'cron_schedules', 'cron_add_weekly' );
    function cron_add_weekly( $schedules ) 
             // Adds once weekly to the existing schedules.
             $schedules['couple_hours'] = array(
                                             'interval' => 7200,
                                             'display' => __( 'Couple Hours' )
             return $schedules;

Register the Hook which will trigger every two hours

    add_action( 'init', 'apto_schedule_event' );
     * On an early action hook, check if the hook is scheduled - if not, schedule it.
    function apto_schedule_event() 
            if ( ! wp_next_scheduled( 'apto_custom_schedule' ) ) 
                    wp_schedule_event( time(), 'couple_hours', 'apto_custom_schedule');

    add_action( 'apto_custom_schedule', 'apto_run_custom_schedule' );

Create the code which will do actual sort randomize. You should replace the $sort_view_id value with the number you found on your side.

     function apto_run_custom_schedule() 
            global $wpdb;
            $sort_view_id = 1822;
            $mySql_query = "SELECT `object_id` FROM `". $wpdb->prefix ."apto_sort_list` 
                                    WHERE sort_view_id  =   '". $sort_view_id  ."'";
            $records = $wpdb->get_results($mySql_query);
            //no need to go further if no record
            if(count($records)  <   1)
            //delete all data
            $mySql_query = "DELETE FROM `". $wpdb->prefix ."apto_sort_list` 
                                    WHERE sort_view_id  =   '". $sort_view_id  ."'";
            $results = $wpdb->get_results($mySql_query);
            //randomise the records
            //reinsert the data into the table
            $mySql_query = "INSERT INTO `". $wpdb->prefix ."apto_sort_list`
                                    (sort_view_id, object_id) 
            foreach($records        as $record)
                    $mySql_query    .=  "  (". $sort_view_id  .", ". $record->object_id  ."),";   
            $mySql_query   =   rtrim($mySql_query, ',');
            $results = $wpdb->get_results($mySql_query);