Documentation - How To - Update Order for Child Categories When Modifying Parent Category Sort Order

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

Update Order for Child Categories When Modifying Parent Category Sort Order

In WordPress, organizing content through categories is a fundamental aspect of structuring your website. When dealing with a hierarchical structure of categories, sorting can be managed individually per each category, depending on the required order.
Sometimes, when changing the sorting for the parent category, it require to reflect for their child categories too. Upon updating the parent category’s sorting, the plugin intelligently applies the same ordering logic to its child categories. Consequently, posts within the child categories will align with the revised order of their parent category, maintaining coherence and consistency throughout your website.

    
    add_action('apto/reorder-interface/order_update_complete',  'parent_term_child_order_update' );
    function parent_term_child_order_update( $sort_view_id )
        {
            global $APTO, $wpdb;
            
            $taxonomy   =   'category';
            
            $sort_view_settings =   $APTO->functions->get_sort_view_settings( $sort_view_id );    
            
            if ( $sort_view_settings['_order_type'] !=  'manual' )
                return;
                
            if ( $sort_view_settings['_view_selection'] !=  'taxonomy'  ||  $sort_view_settings['_taxonomy'] !=  $taxonomy )
                return;
            
            $current_sort_view_id   =   (int)$_POST['sort_view_id'];
            $current_sort_view_object   =   get_post( $current_sort_view_id );
            
            $sortID =   $current_sort_view_object->post_parent;
            
            $current_language   =   $APTO->functions->get_blog_language();
            $term_id            =   $sort_view_settings['_term_id'];
            
            //check if there are any child categories of the current $term_id
            $children_ids = get_term_children( $term_id, $taxonomy );
            
            if ( count ( $children_ids ) < 1 )
                return;
            
            /**
            * Parse the itmes
            */
            $_data      =   explode( "&", $_POST[ 'order_list' ] );   
            $_data      =   array_filter($_data);
            
            $order_list =   array();
            
            foreach ($_data as $_data_item)
                {
                    list($data_key, $value) = explode("=", $_data_item);
                    
                    if ( strpos($data_key, 'item[') === 0 )
                        {
                            $data_key = str_replace("item[", "", $data_key);
                            $data_key = str_replace("]", "", $data_key);
                            $order_list[] = intval ( $data_key );
                        }
                }
            if ( count ( $order_list ) < 1 )
                return;
                
                
            
            $interface_helper     =   new APTO_interface_helper();
                
            foreach ( $children_ids as $children_id )
                {
                    $attr   =   array(
                                        '_view_selection'   =>  'taxonomy',
                                        '_taxonomy'         =>  $taxonomy,
                                        '_term_id'          =>  $children_id,
                                        '_view_language'    =>  $current_language
                                        );
                    
                    $child_sort_view_ID   =   $APTO->functions->get_sort_view_id_by_attributes( $sortID, $attr ); 
                    if ( $child_sort_view_ID == '' )
                        {
                            //create the sort view. 
                            $attr['_order_type']    =   'manual';
                            $child_sort_view_ID       =   $interface_helper->create_view( $sortID, $attr );
                        }
                        
                    $APTO->functions->delete_sort_list_from_table( $child_sort_view_ID );
                    
                    $mysql_query    =   "INSERT INTO `". $wpdb->prefix ."apto_sort_list`
                                                (`id`, `sort_view_id`, `object_id`)
                                                VALUES ";
                    foreach ( $order_list       as  $index )
                        {
                            $mysql_query    .=  "( null, $child_sort_view_ID, $index ),";
                        }
                    $mysql_query    =   rtrim ( $mysql_query , ',' );
                    
                    $wpdb->get_results( $mysql_query );
                }
                
        }

The code should be placed inside within the theme functions.php or a file on /wp-content/mu-plugins/ folder.