Solved

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

Posted on 2014-09-22
4
116 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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now