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 a fixed position for posts can be set independently of newly created items. This feature allows users to prioritize specific posts by pinning them to a designated spot, ensuring they remain prominent regardless of any changes to the rest of the content. Whether for important announcements, featured articles, or critical updates, Sticky Items provide a reliable way to maintain visibility for key posts.

Randomizing items while keeping Sticky Items in place is easy to achieve through a programmable filter. This functionality enables a dynamic display of content, enhancing user engagement by presenting a fresh and varied selection of posts each time, while still preserving the fixed positions of the Sticky Items. By using a programmable filter, developers can customize the randomization process to suit specific needs, ensuring that the overall user experience remains coherent and aligned with the site’s content strategy. This blend of fixed and dynamic elements within the re-order interface offers a versatile approach to content management, balancing stability and variety effectively.

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.