Title: loop through custom terms
Last modified: August 20, 2016

---

# loop through custom terms

 *  [hugoassuncao](https://wordpress.org/support/users/hugoassuncao/)
 * (@hugoassuncao)
 * [14 years ago](https://wordpress.org/support/topic/loop-through-custom-terms/)
 * I’m trying to get a list of terms for a custom loop.
    I’ve tried some forms to
   get that, but can’t figure out what is going whrong.
 * heres the thing:
    there’s a custom post called photos, that has 2 taxonomies:
   photo_year and photo_tags.
 * there are two pages, each one displaying photos from each year.
 * i can get the list of therms for the photo_tags taxonomy with this piece of code:
 *     ```
       $terms = get_terms("photo_tags");
       	$count = count($terms);
       	if ( $count > 0 ){
       	     foreach ( $terms as $term ) {
       	     echo '
       <li><a>slug .'" class="foto-filter-button '. $term->slug .'">'.$term->name.'</a></li>
       ';
       	     }
       	 }
       ```
   
 * but this returns a list of all terms, including those applied to the posts of
   the 2012 year.
 * so, I run out a loop:
 *     ```
       <?php
       query_posts( array (
       'post_type' => 'photo', 'photo_year' => '2011', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1) );
       ```
   
 * and then a code for getting the terms for these posts:
 *     ```
       $terms = wp_get_post_terms($post->ID,'photo_tags');
       	foreach($terms as $term){
       		echo '
       <li><a>slug .'" class="foto-filter-button '. $term->slug .'">'.$term->name.'</a></li>
       ';
       	}
       ```
   
 * this code get the terms, but it gets all of them, repeating a lot (because there
   are single tags applied to multiple posts).
    so, I would like to know how to 
   get the single terms, without repeating each entry every time it apears in a 
   post in the loop.
 * I think that it must be something looking for repeated id’s of the terms or something
   like that.
 * ps.: sorry for the bad english.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [14 years ago](https://wordpress.org/support/topic/loop-through-custom-terms/#post-2776461)
 * You could always directly query the taxonomies table, along the lines of `$wpdb-
   >get_results("SELECT DISTINCT...");`
    Sorry, I don’t know exactly how to get 
   what you need out of the table, but what I’ve shown should get you pointed in
   the right direction.
 *  Thread Starter [hugoassuncao](https://wordpress.org/support/users/hugoassuncao/)
 * (@hugoassuncao)
 * [14 years ago](https://wordpress.org/support/topic/loop-through-custom-terms/#post-2776635)
 * thank you bcworkz, I’ll give a shot.
 *  Thread Starter [hugoassuncao](https://wordpress.org/support/users/hugoassuncao/)
 * (@hugoassuncao)
 * [14 years ago](https://wordpress.org/support/topic/loop-through-custom-terms/#post-2776649)
 * I found a way to get all the terms that I want.
 *     ```
       query_posts( array ('post_type' => 'photo', 'photo_ano' => '2011', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1) );
       	while ( have_posts() ) : the_post();
   
       		$postterms = get_the_terms( $post->ID, 'photo_tags' );
       			if ($postterms) {
       				foreach($postterms as $term) {
       					$all_terms[] = $term->name;
       				}
       			}
       		endwhile;
   
       			$terms = array_unique($all_terms);
       				foreach ($terms as $term) {
       					echo '<li><a href="#'.$term->slug.'" class="foto-filter-button '.$term->slug.'">'.$term.'</a></li>';
       				}
       ```
   
 * but, this solution gives me a one dimensional array, because of the **$all_terms[]
   = $term->name;** piece of code.
    so, I can’t get the slug, just the name of the
   term.
 * is that a way to put two values (name and slug) in that array so I can print 
   out these values separated?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [14 years ago](https://wordpress.org/support/topic/loop-through-custom-terms/#post-2776699)
 * I’m weak on php array manipulation, so I may be wrong, but can’t you do something
   like:
 *     ```
       $all_terms[]['name'] = $term->name;
       $all_terms[]['slug'] = $term->slug;
       ```
   
 * Just an idea, sorry I don’t have a definitive answer.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘loop through custom terms’ is closed to new replies.

## Tags

 * [custom taxonomy](https://wordpress.org/support/topic-tag/custom-taxonomy/)
 * [custom terms](https://wordpress.org/support/topic-tag/custom-terms/)
 * [taxonomy](https://wordpress.org/support/topic-tag/taxonomy/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 4 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [14 years ago](https://wordpress.org/support/topic/loop-through-custom-terms/#post-2776699)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
