Apply post type Archive order for all taxonomies / categories

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

The Advanced Post Types Order plugin is very flexible in functionality. In certain situations, individual sort for each taxonomy terms (categories) is not required, but instead Archive order is needed to apply on all other sections.

The following example return the archive order for any query that match the post type array. The apto_query_match_sort_id filter is being used:


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)
        {
            //no need to apply for admin
            if(is_admin())
                return $new_orderBy; 

                
            //if the's a custom sort, return
            if(!empty($sort_view_id))
                return $sort_view_id;    
            
            
            global $APTO;
            
            $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;
             
        }