Question

I have a custom post type that includes a custom taxonomy, called client_name. There is a page that loads these custom post types and displays them. What I need to develop is a dropdown list that displays all the custom taxonomy of those custom posts, which I have done, but I need to remove duplicates, and arrange them alphabetically, which is where I am having problems.

This is what I am trying for removing duplicates (it doesn't work):

<?php
<form method="post" id="clientform" action="" onsubmit="return getURL(this.url.value)">
  <select name="url" id="client">
    <option selected="selected">By Client</option>
    <?php
      $args=array(    
        'post_type' => 'our_work',
        'post_status' => 'publish',
        'order' => 'ASC',
        'posts_per_page' => -1
      );
      $my_query = new WP_Query($args);
      $k=0;
      if( $my_query->have_posts() ) { 
        while ($my_query->have_posts()) : $my_query->the_post();
          $termArray[$k] = array (trim(get_the_term_list( $post->ID, 'client_name')));
          // $text = trim(get_the_term_list( $post->ID, 'client_name'));
          for ($i = 0; $i <= $termArray.count; $i++ ) {    
            for ($j = 0; $j <= $tempArray.count; $j++) {
              if ($tempArray[$i] == $tempArray[$j]) {        
                unset($tempArray[$k]); 
                $termArray = array_values($termArray);
              }
            } // end of for 
            $k++; 
        ?><option value="<?php bloginfo('url'); echo "/client_name/"; echo $termArray[$k]; ?>"><?php echo $termArray[$k]; ?></option><?php 
        } ?>    
        <?php 
        endwhile; 
      } 
      wp_reset_query(); 
    ?>
  </select>
  <input type="submit" id="clientsubmit" value="Search" />
</form>

Ok, now I ran across this PHP function the other day and it does what I want, I just dont know how to use it correctly.

array_unique($input);

Help me out guys .... Note the code listed above is only one code branch I was trying, not the whole file.


UPDATE

Hey this does help me quite a bit, it and I found this to use, this gets me like 95% done, but not 100% yet. I tried your code, and I tried this one I am using, and it removes the dup's but anything that comes after it wont display hmm....

Heres the WP code:

<?php
if( $my_query->have_posts() ) { 
  while ($my_query->have_posts()) : $my_query->the_post();
    $termArray[$i] = trim(get_the_term_list( $post->ID, 'client_name'));
    $termArray = array_unique($termArray);
    $termArray = array_values($termArray);
    print_r($termArray);  
    print_r(sizeof($termArray));
    if ($termArray[$i] != '') { 
      ?><option value="<?php bloginfo('url'); echo "/client_name/"; echo $termArray[$i]; ?>"><?php echo $termArray[$i]; ?></option><?php 
    }// end of if $i++; 
  endwhile; 
} 
wp_reset_query();

And here is the output from the array, it duplicates the array output because it is called every time a new post is called within the loop. The page is available here:

http://magicvideo.com/cms/work/

View the source to see what i mean.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top