Solved

How to have one taxonomy/ category first in the list then the rest order alphabetically

Posted on 2014-09-22
4
119 Views
Last Modified: 2014-10-24
Hello,

I want to be able to select a category/taxonomy to put first in a list, then show the rest ordered alphabetically.

Can anyone point me in the direction of a wordpress function or filter to achieve this please?

eg. on a country drop down in a search form:

England
Afghanistan
Albania
Andorra
Antarctica
Argentina...
0
Comment
Question by:igloobob
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40336431
Not sure about the topics you've listed here, but in SQL Server I pull this off by adding an < ALL > selection, UNIONing it with the rest of the list, and using a sort_order column to get the sorting correct.  
SELECT id, name, 1 as sort_order
FROM your_table
WHERE id= 42   -- 'or whatever id is England'
UNION ALL
SELECT id, name, 2 as sort_order
FROM your_table
WHERE id <> 42
ORDER BY sort_order, name

Open in new window

Then in your front-end just display the id and name columns.
0
 

Author Comment

by:igloobob
ID: 40390707
I got this working with a little help from a friend as follows...

add_filter('dropdown_sort','terms_sort','',2);
function terms_sort( $terms, $taxname )
{ 
  if($taxname === 'countries')
  {   
    $_a   = array();
    $_n   = array();
    foreach( $terms as $lterm )
    {
      if($lterm->name === 'Country  To Appear First')
      {
        $_n[$lterm->name] = $lterm;
        continue;
      }
      $_a[$lterm->name] = $lterm;
    }
    ksort($_a);
    return ($_n + $_a); 
  }
  return $terms;
}

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question