How to Randomize order but keep in place the Sticky Items

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

Setting Sticky Items within the re-order interface is a cool plugin feature, through which fixed position for posts can be set, independently of sorting or newly created items.
Randomizing items while keeping in place the Sticky Items is each to achieve through a programmable filter.

The following code should be placed within a file, inside /wp-content/mu-plugins/ folder:

    add_filter( 'apto/order_list/apply_sticky_data',     '_custom_apto_order_list_apply_sticky_data', 10, 4 );
    function _custom_apto_order_list_apply_sticky_data ( $updated_order_list, $order_list, $sticky_data, $sort_view_id )
        {
            global $wpdb;
            
            if ( ! is_array ( $updated_order_list ) ||  count ($updated_order_list ) < 1 || $updated_order_list === FALSE )
                return $updated_order_list;
            
            //Only on front side
            if  ( is_admin() ) 
                return $updated_order_list;
                
            $updated_order_list =   array();
            $randomize_order_list =   $order_list;
            shuffle ( $randomize_order_list );
            
            //insert the sticky items
            foreach($sticky_data as $key =>  $object_id)
                {
                     if(array_search($object_id, $randomize_order_list)   !== FALSE)
                        $updated_order_list[$key - 1]  =   $object_id;   
                }
            
            
            $current_index = 0;
            foreach( $randomize_order_list as $key =>  $object_id)
                {
                    if(array_search($object_id, $updated_order_list)   !== FALSE)
                        continue;
                    
                    while(isset($updated_order_list[$current_index]))
                        {
                            $current_index++;
                        }
                        
                     $updated_order_list[$current_index]  =   $object_id;   
                }
                
            ksort($updated_order_list);
                        
            return $updated_order_list;   
        }

Alternatively, this functionality can also be achieved through user function callback, Automatically Randomize a sort list while keeping the sticky post.