Sort search results by select attribute

Permalink Browser Info Environment
I've added a select Attribute to my real estate listings and the client wants the search results to initially sort by quality: Signature, Executive, Premium, Quality. This is not the case for search results. They are being sorted alphabetically instead. How can I change the search results to sort by the Attribute search rankings?

Type: Discussion
Status: New
TravelNet
View Replies:
Tony replied on at Permalink Reply
Tony
There should be a few options for sorting, but the default package doesn't offer the ability to sort by attribute like quantity, so this is something you'd have to add yourself. Take a look within the real estate search block's controller within the package to see how the other sorting is done. I'll try to look at the code this weekend for you too to see if I can get you a more specific code snippet to put in there.
TravelNet replied on at Permalink Reply
TravelNet
Hi Tony,

Have you had a chance to look into the support issue?
Tony replied on at Permalink Reply
Tony
there's this commented out section within /packages/tony_real_estate/blocks/tony_real_estate_search/controller.php:

}elseif( substr($sortBy,0,4)=='fak_' ){
     $fakID=substr($sortBy,4); 
     $ak =  CollectionAttributeKey::get( intval($fakID) ); 
     if(is_object($ak)) $pagelist->sortByAttributeKey( $ak, $sortAscDesc);


uncommenting that snippet will enable sorting by custom attributes. Notice that the id needs to be in the format of fak_123, where 123 is the attribute key id.

You'll also want to add that option up above where the available sorting options are defined:

public function getDefaultSortableOpts(){
      $details=array();
      //$details['title'] = t('Title');
      $details['newest'] = t('Newest');
      $details['oldest'] = t('Oldest');      
      $details['price'] = t('Price: Low to High');
      $details['price_desc'] = t('Price: High to Low');
      $details['alpha_asc'] = t('Alphabetical'); //(must use page name as title)  
      $details['display_order'] = t('Display Order'); //(sorted within sitemap)  
      $details['fak_123'] = t('My Attribute Name');   
      return $details;
   }
TravelNet replied on at Permalink Reply
TravelNet
I tried changing my code but nothing changed.

Here is what I changed it to:

elseif( substr($sortBy,0,4)=='fak_' ){
            $fak_69=substr($sortBy,4); 
            $ak =  CollectionAttributeKey::get( intval($fak_69) ); 
            if(is_object($ak)) $pagelist->sortByAttributeKey( $ak, $sortAscDesc);  
         }elseif($sortBy=='oldest'){ 
            $pagelist->sortBy('cDateAdded', 'asc');  
         }elseif($sortBy=='fak_69'){
$ak =  CollectionAttributeKey::getByHandle( 'fak_69' ); 
            if(is_object($ak)) $pagelist->sortBy( $ak, 'desc'); }
elseif($sortBy=='sleeps'){
$ak =  CollectionAttributeKey::getByHandle( 'sleeps' ); 
            if(is_object($ak)) $pagelist->sortBy( $ak, 'desc'); }
elseif($sortBy=='bedrooms'){
$ak =  CollectionAttributeKey::getByHandle( 'bedrooms' ); 
            if(is_object($ak)) $pagelist->sortBy( $ak, 'desc'); }


Am I missing something or did I do something wrong?

I also noticed that now if I change the Sort By option I get different results for the order of Quality properties. They are still not in order of Quality. Here is an example:http://www.bbranch.vemglobal.net/lodging/vacation-rental-search/vac...
Tony replied on at Permalink Reply
Tony
Can you tell if it's getting of those elseif condition sections? You could test it by adding some echo $sortBy; and die; statements to each section.

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.