Solved

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

Posted on 2014-09-22
4
120 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
[X]
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
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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

737 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