Title: Taxonomy in loop
Last modified: August 20, 2016

---

# Taxonomy in loop

 *  [Amit Kvint](https://wordpress.org/support/users/webifish/)
 * (@webifish)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/)
 * Hi there,
 * So I have created 2 new taxonomies in my fuctions.php :
 * // Adding Taxonomy !
 * function create_my_taxonomies() {
    register_taxonomy(‘locations’, ‘post’, array(‘
   hierarchical’ => true, ‘label’ => ‘Locations’, ‘query_var’ => true, ‘rewrite’
   => true));
 *  register_taxonomy(‘activities’, ‘post’, array(
    ‘hierarchical’ => true, ‘label’
   => ‘Activities’, ‘query_var’ => true, ‘rewrite’ => true)); }
 * add_action(‘init’, ‘create_my_taxonomies’, 0);
 * ?>
 * And now I try to use them in my loop like that –
 * <?php query_posts(array(‘locations’ => ‘vejer’)); ?>
    <?php if (have_posts()):?
   > <?php while (have_posts()) : the_post(); ?> <h2> “ rel = “bookmark” title =“
   <?php the_title_attribute(); ?>” alt = “ <?php the_title_attribute(); ?>” > <?
   php the_title(); ?>  </h2> <?php endwhile; ?> <?php else : ?> <h2>Sorry nothing
   Found</h2> <?php endif; ?>
 * This works perfect, but I have a simpler case that i can’t seem to sort out –
   what if I want to display all the posts that have taxonomy ‘locations’ ?
 * Thanks,
    Amit

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

 *  [Maidul](https://wordpress.org/support/users/maidulcu/)
 * (@maidulcu)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994181)
 * Hi,
    in the query_posts add posts_per_page = -1 Hope this will help [http://codex.wordpress.org/Function_Reference/query_posts](http://codex.wordpress.org/Function_Reference/query_posts)
   Thanks
 *  Thread Starter [Amit Kvint](https://wordpress.org/support/users/webifish/)
 * (@webifish)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994241)
 * Maidul,
 * Wouldn’t that just show all posts ? I am looking for something that will show
   all posts with the specific ‘location’ taxonomy.
 * Thanks
    Amit
 *  [Maidul](https://wordpress.org/support/users/maidulcu/)
 * (@maidulcu)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994248)
 * Hi Amit,
 * This should display posts tagged with ‘vejer’, under ‘locations’ custom taxonomy
   
   query_posts(array(‘locations’ => ‘vejer’, ‘posts_per_page’ => -1 ));
 * Let me know the outcome
 * Thanks
 *  Thread Starter [Amit Kvint](https://wordpress.org/support/users/webifish/)
 * (@webifish)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994251)
 * Thanks Maidful,
 * But this shows all post that are under ‘location’ with the value of ‘vejer’ (‘
   locations’ => ‘vejer’) & I am looking for something that will show all posts 
   that are under locations, any idea ?
 * Amit
 *  [Maidul](https://wordpress.org/support/users/maidulcu/)
 * (@maidulcu)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994254)
 * Then change this to
 * `query_posts(array('taxonomy' => 'locations', 'posts_per_page' => -1 ));`
 * Thanks
 *  Thread Starter [Amit Kvint](https://wordpress.org/support/users/webifish/)
 * (@webifish)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994256)
 * Hey,
 * Yeah I know, have tried that too…but I get also posts that are defined as ‘activities’…(
   set in functions.php as I mentioned earlier) don’t know why…
 * Amit
 *  [Maidul](https://wordpress.org/support/users/maidulcu/)
 * (@maidulcu)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994261)
 * Hi Amit,
 * Can you try this one
 *     ```
       $args = array(
       	'tax_query' => array(
       		array(
       			'taxonomy' => 'locations'
       		)
       	)
       );
       $the_query= new WP_Query( $args );
   
       // The Loop
       while ( $the_query->have_posts() ) : $the_query->the_post();
       	echo '<li>';
       	the_title();
       	echo '</li>';
       endwhile;
   
       // Reset Post Data
       wp_reset_postdata();
       ```
   
 * Thanks
 *  Thread Starter [Amit Kvint](https://wordpress.org/support/users/webifish/)
 * (@webifish)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994268)
 * Maidul,
 * Thanks : )…
 * But now no post titles are shown…no error though
 * Amit
 *  [Maidul](https://wordpress.org/support/users/maidulcu/)
 * (@maidulcu)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994277)
 * Where did you place this loop?
 *  Thread Starter [Amit Kvint](https://wordpress.org/support/users/webifish/)
 * (@webifish)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994281)
 * Maidul,
 * What do you mean ? it goes in the index.php in a div I have made on the bottom
   for these links, here take a look at the URL – [http://educational-adventures.org/ea/](http://educational-adventures.org/ea/)
 * You can see the post for vejer (‘location’ => ‘vejer’) I have replaced that line
   with the new vars & arrays you suggested
 * Amit
 *  [Maidul](https://wordpress.org/support/users/maidulcu/)
 * (@maidulcu)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994287)
 * Did you properly coded the loop ?
 * Please find all the info here
 * [http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters](http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters)
 * I am confused why this is not working
 * Let me know
 * Thanks
 *  Thread Starter [Amit Kvint](https://wordpress.org/support/users/webifish/)
 * (@webifish)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994302)
 * I think so,
 * I will take another look but first… a break,
 * Me too
 * Thanks,
 * Amit
 *  Thread Starter [Amit Kvint](https://wordpress.org/support/users/webifish/)
 * (@webifish)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994342)
 * So I have found that little script –
 *     ```
       <?php
       $taxonomies = get_the_term_list($post->ID, 'locations', '', '', '');
       $taxonomies = explode('>', $taxonomies);
       $taxonomies = $taxonomies[1];
       $myq = new WP_Query('locations = '.$taxonomies);
       if ($myq->have_posts()) : while ($myq->have_posts()) : $myq->the_post(); ?>
   
       		<h2>
       		<a href = "<?php the_permalink(); ?>"
       		rel = "bookmark" title = "
       		<?php the_title_attribute(); ?>"
       		alt = "
       		<?php the_title_attribute(); ?>"
       		>
       		<?php the_title(); ?>
       		</a>
       		</h2>
       <?php endwhile; else:?>
   
       <?php endif;?>
       ```
   
 * But still no luck – it shows all posts doesn’t mattar what taxonomy they are 
   attached to, I will give up @ the end…
 * Amit

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

The topic ‘Taxonomy in loop’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 13 replies
 * 2 participants
 * Last reply from: [Amit Kvint](https://wordpress.org/support/users/webifish/)
 * Last activity: [13 years, 8 months ago](https://wordpress.org/support/topic/taxonomy-in-loop/#post-2994342)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
