Apply post type Archive order to all taxonomies / categories

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

The Advanced Post Types Order plugin is an incredibly powerful tool that allows WordPress users to easily sort their posts, pages, custom post types, and WooCommerce products. It’s a fantastic way to organize content on your website, but did you know that the plugin is also incredibly flexible in terms of its functionality? In certain situations, you may not need individual sorts for each taxonomy term, and instead, an Archive order is needed to apply on all other sections.

For example, let’s say you have a blog with multiple categories, and you want to display the posts in a specific order. One way to achieve this is to create an individual sort for each category, but this can be time-consuming and inefficient. Instead, you can use the Archive order to apply the same sorting order to all categories.

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()   &&  ! defined ( 'DOING_AJAX' ) )
                return $sort_view_id;

            global $APTO;

            $query_post_types   =   $APTO->functions->query_get_post_types( $query, TRUE );

            $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 $sort_view_id;

            if  (   is_array( $query_post_types  ) &&   count ( $query_post_types  ) > 1  )
                return $sort_view_id;
            
            if  (   ! is_array( $query_post_types  ) && empty  ( $query_post_types  ) )
                return $sort_view_id;
            
            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;

        }