Use a simple sort along with complex queries

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

Advanced Post Types Order Simple Sort is an easy way to maintain order for different queries/sections (e.g. archive, taxonomies) within a single interface. As opposite an Advanced Sort can match a single query but which include a mix of posts, taxonomies, meta data and other parameters. This is a powerful tool which allow to match a sort virtually for any WordPress Queries, however it require specific sort settings to match exactly the front site query parameters. For that reason it can get a bit hard to manage for dynamic section which use a large number of complex queries.

Advanced sorts are not necessarily to be used, order can be created just for a specif taxonomy (e.g. attribute or category) and force the sort to apply for complex queries, which can contain other parameters like more taxonomies.

Let’s take a particular scenario, a WooCommerce instance with a custom filter plugin which output the products based on category and a specific attribute. This would require an advanced sort to be created for every combination (category and attribute), however this will get hard to manage at a point due to high number of sort lists. To make things much easier, we can crate a single simple sort and force the attribute order to apply to all queries which include a category and an attribute taxonomy property.
The attribute is called pa_drinks and is being passed through GET data. For food category a filter link would appear as the following:


Naturally this would require an advanced sort and set the 2 taxonomies, the category and the attribute. However we can force to fallback on the simple sort instead:


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
                return $new_orderBy; 
            global $APTO;
            //This is the simple sort id which intend to be used instad
            $reference_sort_id  =   5529;
            //retrieve the query post types arguments
            $query_post_types   =   $APTO->functions->query_get_post_types($query);
            //we need products only
            if(count($query_post_types)  > 1 || !in_array("product", $query_post_types))
                return $sort_view_id;
            //Only when pa_drinks is in the GET
            //Other attributes can be included or removed alltogheter
                return $sort_view_id;
            //populate attribute details    
            $pa_drinks    =   $_GET['pa_drinks'];
            $pa_drinks_term   =   get_term_by('slug', $pa_drinks, 'pa_drinks');
            //this will match only queries with multiple taxonomies
            if(APTO_query_utils::tax_queries_count($query->tax_query->queries) < 2)
                return $sort_view_id;
            //make a filtred list of query taxonomies
            $taxonomies =   APTO_query_utils::get_tax_queries($query->tax_query->queries);    
            $found  =   FALSE;
            //Identify if the query actually include a taxonomy of pa_drinks
            foreach($taxonomies as  $taxonomy)
                    if($taxonomy['taxonomy']    !=  'pa_drinks')
                    if(!in_array($pa_drinks_term->slug, $taxonomy['terms']))
                    $found  =   TRUE;
            if( $found === FALSE )
                return $sort_view_id;;
            //this match.
            $attr = array(
                            '_view_selection'       =>  'taxonomy',
                            '_taxonomy'             =>  'pa_drinks',
                            '_term_id'              =>  $pa_drinks_term->term_id,
                            '_view_language'        =>  $APTO->functions->get_blog_language()  
            //Retrieve the sort_view_id whcih correspond to the pa_drinks term                                    
            $sort_view_id   =   $APTO->functions->get_sort_view_id_by_attributes($reference_sort_id, $attr);
            return $sort_view_id;

Further, adjusting the sort on the simple sort, through the attribute, the order will apply on the front side for any queries which include the attribute.

Notify of
Inline Feedbacks
View all comments