Autosort a Category by posts Featured Image Title

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

The Automatic Order functionality along with Custom Function it’s a very powerful tool to customize the sort of a give area by very specific criteria. As specified at https://www.nsp-code.com/applying-automatic-order-for-a-sort-through-a-user-function-callback/ the filter return a list of all posts within the section / query on which different operations can apply.

In the following example, a sort list is ordered by the posts Featured Images Title. After creating a simple sort as show at https://www.nsp-code.com/advanced-post-types-order-description-and-usage/ chose Automatic Order, then Custom Function. As parameter, use function name which should be included within theme functions.php or a custom plugin, in this example custom_sorting_function_featured_file

function custom_sorting_function_featured_file($post_list, $sort_view_id, $orderBy, $query)
    {
        $list_map_feature   =   array();
                foreach($post_list  as  $object_id)
                    {
                        $thumbnail_id = get_post_meta($object_id, '_thumbnail_id', TRUE);
                        
                        if(empty($thumbnail_id))
                            continue;
                            
                        $feature_post   =   get_post($thumbnail_id);
                        
                        $list_map_feature[basename($feature_post->guid)]    =   $object_id;
                    }
                     
                //sort the dates ascending
                ksort($list_map_feature);
                 
                $post_list  =   array_values($list_map_feature);
                     
                return $post_list;  
    }