Title: Call data from another post type inside a loop
Last modified: August 21, 2016

---

# Call data from another post type inside a loop

 *  [obidos](https://wordpress.org/support/users/obidos/)
 * (@obidos)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/call-data-from-another-post-type-inside-a-loop/)
 * Hi all,
 * I have a little WordPress issue and your help would be amazing.
 * Basically I have two custom post types one called Genres and the other called
   Testimonials. I am listing all the genres on the web page. This works well.
 * However, I want to call all Testimonials relating to the certain genre.
 * The testimonials has a custom taxonomy called genrescategory.
    So each time the
   admin add a testimonial they assign this is a genre.
 * What I want to happen is within the loop calling all genres in the genres custom
   post type to call all the testimonials assigned to that genere name e.g. animation
 * Here is the loop to get all genres:-
 *     ```
       <?php
         $args = array(
       	  'post_type' 		 => 'genres',
       	  'posts_per_page'    => -1
         );
         $posts=get_posts($args);
   
         if ($posts)
         {
       	  foreach($posts as $post)
       	  {
       		  setup_postdata($post);
       ?>
       <div id="<?php the_field('ancher_name'); ?>" class="genres-container">
         <h2 class="icon">
       	  <img src="<?php the_field('genres_what_we_do_page_icon'); ?>" width="66" height="64" alt="Animation">
       	  <?php the_title(); ?>
         </h2>
         <p><?php the_content(); ?></p>
   
         <div class="whowetalkto">
       	  <h3>Who we talk to...</h3>
       	  <?php if(get_field('who_we_talk_to')):  ?>
       		  <?php while(has_sub_field('who_we_talk_to')): ?>
       	  <div class="circular">
       		  <img src="<?php the_sub_field('profile_picture'); ?>" alt="<?php the_sub_field('full_name'); ?>">
       		  <figcaption><?php the_sub_field('full_name'); ?></figcaption>
       	  </div>
       	  <?php endwhile; ?>
       	  <?php endif; ?>
         </div>
   
         <!-- Add in testimonials for current genres in here -->
   
       </div>
       <?php
         }
       }
       ?>
       ```
   

Viewing 1 replies (of 1 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/call-data-from-another-post-type-inside-a-loop/#post-4040905)
 * I do not have a way to test this, but it should be close to what you want. I 
   assume that the field ‘ancher_name’ holds the name of the genre. If not, you 
   will need to modify the line that sets $genre.
 * Insert this code after the comment ‘Add in testimonials …’:
 *     ```
       <?php
          $genre = get_field('ancher_name');
          $args = array(
       	   'post_type' 		 => 'testimonials',
       	   'posts_per_page'    => -1,
       	   'genrescategory' => $genre
          );
          $testimonials = new WP_Query($args);
   
          if ($testimonials->have_posts()) {
       	   while ($testimonials->have_posts()) {
       	      $testimonials->the_post();
       		   // Show the testimonial here
       		   ?>
       		   <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
       		   <?php
       		}
       	}
       	?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Call data from another post type inside a loop’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [12 years, 9 months ago](https://wordpress.org/support/topic/call-data-from-another-post-type-inside-a-loop/#post-4040905)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
