Update Archive list order when changing the sort for a category

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

The Advanced Post Types Order plugin excels in managing the order of all sections and areas across a website, ensuring precise customization for each section based on specific requirements.

In some cases, it’s essential to automatically update the global list (Archive) when the order of a category is modified. To achieve this, establish a logic to maintain the Archive selection in sync with changes in the category order. One effective method is to create an order for the category list using the Category Order and Taxonomy Terms Order plugin. This ordered category list is then used to build the Archive order.

Here’s an example: Suppose there are categories like Book, Blog, and Art. Any change in the sorting of these categories, whether through the re-order interface or the default WordPress interface, triggers an update in the Archive. When a category (e.g., Art) is selected through the default WordPress interface:

Through the default WordPress interface a category is selecected, in our example Art:

Simply drag and drop the posts as needed. The current category order is updated, and thanks to the custom code, the Archive order is dynamically reconstructed based on the current sorting of categories. In this example, it fetches the Book, then Blog, then Art categories and updates the Archive accordingly.

The following code example can be used for Posts object type and Category taxonomy, but it can be updated for anything else to accommodate any scenario.

<?php
    
        add_action('apto/reorder-interface/order_update_complete', 'custom_apto_order_update_complete');
        add_action('apto/default-interface/order_update_complete', 'custom_apto_order_update_complete');
        function custom_apto_order_update_complete( $sort_view_id )
            {
                global $APTO;
                 
                $sort_view_settings =   $APTO->functions->get_sort_view_settings( $sort_view_id );
                
                $sort_view_data     =   get_post($sort_view_id);
                if($sort_view_data->post_parent > 0)
                    $sortID             =   $sort_view_data->post_parent;
                    else
                    $sortID             =   $sort_view_id;
                
                if ( $sort_view_settings['_order_type'] ==  'manual'  &&  $sort_view_settings['_view_selection'] ==  'taxonomy' &&  $sort_view_settings['_taxonomy'] ==  'category' )
                    {
                        $attr = array(
                                    '_view_selection'       =>  'archive',
                                    '_view_language'        =>  $APTO->functions->get_blog_language()
                                    );
                        $sort_view_archive_id   =   $APTO->functions->get_sort_view_id_by_attributes ( $sortID, $attr );
                        
                    
                        $order_list         =   $APTO->functions->get_order_list( $sort_view_archive_id );
                        
                        $current_term_id    =   $sort_view_settings['_term_id'];
                        
                        $new_order_list     =   array();
                        
                        //reconstruct the order of the archive list. 
                        $args   =   array(
                               'taxonomy'   =>  'category',
                               'orderby'    =>  'term_order',
                               'order'      =>  'ASC'
                               );
                        $terms  =   get_terms( $args );
                        
                        foreach ( $terms    as  $term )
                            {
                                            
                                //retrieve the order list by query to ensure all posts are returned
                                $args   =   array ( 
                                                    'post_type' =>  'post',
                                                    'orderby'   =>  'menu_order',
                                                    'order'     =>  'ASC',
                                                    'posts_per_page'    =>  -1,
                                                    'fields'    =>  'ids',
                                                    'tax_query'     => array(
                                                                            array(
                                                                                    'taxonomy' => 'category',
                                                                                    'field' => 'id',
                                                                                    'terms' => $term->term_id,
                                                                                    ),
                                                                            ),
                                                    );
                                $custom_query       =   new WP_query( $args );
                                
                                $new_order_list =   array_merge( $new_order_list, array_values( $custom_query->posts ) );
                            }
                            
                        //update the archive list
                        foreach ( $order_list   as  $key    =>  $value )
                            {
                                if ( array_search ( $value, $new_order_list ) !==   FALSE )
                                    unset ( $order_list[ $key ] );
                            }
                            
                        $order_list =   array_merge ( array_values ( $new_order_list ), $order_list );
                        
                        
                        //update the database
                        $APTO->functions->delete_sort_list_from_table( $sort_view_archive_id );
                        
                        global $wpdb;
                        
                        $mysql_query    =   "INSERT INTO `". $wpdb->prefix ."apto_sort_list`
                                                      (id, sort_view_id, object_id)
                                                    VALUES ";
                        $first = TRUE;
                        foreach( $order_list as $post_id )
                            {
                                if($first === TRUE)   
                                    $first  = FALSE;
                                    else
                                    $mysql_query    .=  ", \n";
                                    
                                $mysql_query  .= "(null, ". $sort_view_archive_id .", ". $post_id .")";
                            }
                        $results = $wpdb->get_results( $mysql_query );
                    }
            }
        

The above code example should be placed inside your child theme functions.php or a custom file on your /wp-content/mu-plugins/ folder.

In the described logic, the customized Archive order will be overwritten whenever any changes are made to the order of categories.