Filter – Applying a different sort for certain sections / queries

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


This filter can be used to apply a different sort to a given query.

apto_get_orderby

Examples

The following code will change the returned sort for all terms archives and use the Archive sort list, similar to free Post Types Order plugin.

add_filter('apto_get_orderby', 'theme_apto_get_orderby', 10, 3);
function theme_apto_get_orderby($new_orderBy, $orderBy, $query)
    {
        global $wpdb;
        
	//no need to apply for admin
        if(is_admin())
            return $new_orderBy; 

        //identify the order type automatic or manual
        $order_type = apto_get_order_type($query);
        
        //we don't want to change an automatic order
        if($order_type == 'auto')
            return $new_orderBy;
            
        list($post_type, $taxonomy) = apto_get_query_post_type_taxonomy($query);   
        
        if($taxonomy != "_archive_")
            {
                $taxonomy   = "_archive_";
                $term_id    = -1;
                
                //fetch the order list 
                $order_list  = apto_get_order_list($post_type, $term_id, $taxonomy, $query);
                
                if (count($order_list) > 0 )
                    {
                        $new_orderBy = "FIELD(".$wpdb->posts.".ID, ". implode(",", $order_list) ."), ".$wpdb->posts.".post_date DESC";
                    }
                    else
                    {
                        $new_orderBy = $wpdb->posts.".menu_order, " . $wpdb->posts.".post_date DESC";
                    }
                
            }
        
        return  $new_orderBy;  
    }

*This code should be used within the theme or a custom plugin.

API Index