1

I am builing a search engine where I need to search in 2 different taxonomies and sort them by slug. I got this code to do so.

$search = sanitize_text_field( $_POST[ 'search' ] );
$content = '';
$terms = get_terms(array(
    'taxonomy' => array('apartments','houses'),
    'search' => $search,
    'orderby' => 'name',
    'order'             => 'DESC',
    'hide_empty' => true
));

Right now the result is:

  • Apartment A
  • Apartment B
  • House A
  • House B

and i would like it to be like that:

  • Apartment A
  • House A
  • Apartment B
  • House B

Any idea as I run out of at the moment ;) I would like to avoid JS at that moment.

4
  • I'm unsure what kind of order that is, is it reverse order aka A esuoH? This ordering doesn't make sense, are the terms grouped together in some other way that isn't being shared?
    – Tom J Nowell
    Dec 6, 2021 at 18:25
  • I don't think there's an orderby parameter that would order the results as you've described. But you could use usort to implement custom order after the terms have been retrieved
    – admcfajn
    Dec 6, 2021 at 18:32
  • @admcfajn That was a great idea. This helped do the trick.
    – Mastafu
    Dec 6, 2021 at 19:33
  • 1
    Awesome, glad you got it working @Mastafu
    – admcfajn
    Dec 6, 2021 at 21:38

1 Answer 1

1
$keys = array_column($terms, 'slug');
array_multisort($keys, SORT_ASC, $terms);

That did the trick and let me sort terms in that muli array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.