Apply Archive Order to Taxonomies Terms / Categories, if no custom order is defined

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

When migrating from free Post Types Order, on activation. the Advanced Post Types plugin automatically import the previously customized order. This is saved within Archive area, since this is the type of sorting the free code is capable off. The other areas as taxonomies terms like categories, inherit default sorting by date as there’s never been applied a customized order.

To apply the Archive order to all areas just like the Post Types Order does, but still allow and apply any customized sorting (e.g. a specific category), the following code can be used. This should be placed within theme functions.php or a custom plugin.

    add_filter('apto/query_match_sort_id',  'apto_query_match_sort_id', 10, 4); 
    function apto_query_match_sort_id($sort_view_id, $orderBy, $query, $sorts_match_filter)
        {
            global $APTO;
            
            //if the's a custom sort, return
            if(!empty($sort_view_id))
                {
                    //ensure the list is not empty
                    $order_list  = $APTO->functions->get_order_list($sort_view_id);
                    if  ( is_array( $order_list ) &&  count ( $order_list )     >   1 )
                        return $sort_view_id;    
                }
            
            $query_post_types   =   $APTO->functions->query_get_post_types($query);
            
            $sorts_match_filter =   array(
                                            '_autosort'     =>  array('yes'),
                                            '_view_type'    =>  array('multiple')
                                            );
            $sort_items =   $APTO->functions->get_sorts_by_filters($sorts_match_filter);
            
            if(count($sort_items) < 1)
                return;
            
            foreach($sort_items as  $sort_item)
                {
                    $sort_settings  =   $APTO->functions->get_sort_settings($sort_item->ID);
                    ///check the sort rules if match the post types
                    $sort_rules =   $APTO->functions->get_sort_current_language_rules($sort_settings);
                    
                    //compare the post type args
                    $differences = array_diff($query_post_types, $sort_rules['post_type']);
                    if(count($query_post_types) != count($sort_rules['post_type']) || count($differences) > 0)
                        continue;
                    
                    //this match. Get the archive sort view
                    $attr = array(
                                    '_view_selection'       =>  'archive',
                                    '_view_language'        =>  $APTO->functions->get_blog_language()  
                                    );
                                                       
                    $sort_view_id   =   $APTO->functions->get_sort_view_id_by_attributes($sort_item->ID, $attr);
                    
                    break;
                }
            
            return $sort_view_id;
             
        }

If the order is updated for any term, this is being used instead rater applying the default Archive.