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

Posted on 2014-09-22
Medium Priority
Last Modified: 2014-10-24

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:

Question by:igloobob
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 66

Accepted Solution

Jim Horn earned 2000 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'
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.

Author Comment

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

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;
      $_a[$lterm->name] = $lterm;
    return ($_n + $_a); 
  return $terms;

Open in new window


Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

771 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