Removing Duplicate Custom Taxonomy Terms from within a Dropdown Select?

Hi @Hunter Brelsford:

Good to see you here over from the WordPress Group on LinkedIn.

Maybe I misunderstand your question but it sounds like you are simply trying to get rid of dups in an array? For example, let’s say you have an array clients:

<?php
$clients = array(
  'Jones Construction',
  'Smith Wholesale',
  'Smith Wholesale',
  'Williams Dry Cleaning'
);

And you want to convert to an array like this?

<?php
$clients = array(
  'Jones Construction',
  'Smith Wholesale',
  'Williams Dry Cleaning'
);

(If yes, that’s a PHP question and not a WordPress question, normally something we send away, but I’ll go ahead and answer here anyway.)

Tis easy; since array keys are unique in PHP just flip the array (exchange the values with their keys) then return the array keys and you’ll have your unique array, like so:

<?php
$clients = array(
  'Jones Construction',
  'Smith Wholesale',
  'Smith Wholesale',
  'Williams Dry Cleaning'
);
print_r(array_keys(array_flip($clients)));

That code prints:

Array
(
    [0] => Jones Construction
    [1] => Smith Wholesale
    [2] => Williams Dry Cleaning
)

Was that what you were after?

UPDATE:

Hi @Hunter Brelsford:

I’m responding to your update. Okay, why don’t we try and tackle this a different way? Here’s a standalone example you can copy to the root of your website as test.php and then run it as http://magicvideo.com/test.php to see it work:

<?php
include "wp-load.php";
header('Content-Type:text/plain');
global $wpdb;
$sql = <<<SQL
SELECT DISTINCT
  tt.term_id
FROM {$wpdb->posts} p
  INNER JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id
  INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE 1=1
  AND p.post_status="publish"
  AND p.post_type="our_work"
  AND tt.taxonomy='client_name'
SQL;
$terms = $wpdb->get_results($sql);
$term_ids = array();
foreach($terms as $term) {
  $term_ids[] = $term->term_id;
}
$terms = get_terms('client_name',array(
  'include'=> implode(',',$term_ids),
));
print_r($terms);

We use raw SQL to query the terms in the taxonomy='client_name' for 'post_type="our_work" and then we collect the $term->term_ids to filter the list of taxonomy terms. I used raw SQL because WordPress doesn’t provide a good way to get this data through an API, at least not that I could find on short notice (if someone else knows of a better way via the API that doesn’t require loading a lot more data than necessary please let me know).

Hopefully this will suggest an approach so that you’ll be able to use this code in your example? Let me know if know if not.