Solved

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

Posted on 2014-09-22
4
121 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 66

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The viewer will learn how to dynamically set the form action using jQuery.

728 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