1

I have 2 custom post types created: "classes" and "teachers". I have created using Advanced Custom Fields plugin a field for "classes" custom post type, a multiselect that helps me add certain existing "teachers" to each class. The multiselect field returns IDs of those teachers. So when I go on single-classes.php it's easy to list for the current class all the teachers that I have selected.

Situation I am now on single-teachers.php and I'm trying to list for current teacher all the "classes" he has been selected on with that multiselect field. All this inside the sidebar. So what I did was do a loop through all the classes with another loop inside that goes through the teachers. When the ID of one of those teachers equals the ID on the current teacher (since I'm on single-teachers.php) then show the title of the class. Here is the code:

<?php 
 $current_post_id = $post->ID; // Get current teacher id
 $num = -1;
 $k=0;
 // loop throught the classes
 $args='&suppress_filters=true&posts_per_page='.$num.'&post_type=classes
&order=DESC&orderby=date';  

         $cust_loop = new WP_Query($args); 
         if ($cust_loop->have_posts()) : 
         while ($cust_loop->have_posts()) : $cust_loop->the_post();   
                     $teachers_list = get_field('teachers');
                     if($teachers_list !="") { 

                     $inner_args = array(
                     'posts_per_page'   => -1,
                     'offset'           => 0,
                     'category'         => '',
                     'category_name'    => '',
                     'orderby'          => 'name',
                     'order'            => 'ASC',
                     'post__in'        =>  $teachers_list,
                     'post_type'        => 'teachers',
                      );
                     $wp_query = new WP_Query($inner_args);
                     // loop throught the teachers of each class
                     // if one of those teachers has the id equal with the current 
                     // teacher id then show the title of the class
                     while ($wp_query->have_posts()) : $wp_query->the_post(); 

                            if( $current_post_id === $post->ID ) { 
                                   $k = 1;
                              }

                     endwhile;

                     wp_reset_postdata(); 

                     }

                    if ($k==1) {  ?>

                          <a class="post-title" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> 

                    <?php $k=0; 
                         }
                     endwhile; 
                     endif; 
                     wp_reset_query(); ?> 

Problem is this

                      <a class="post-title" href="<?php the_permalink(); ?>" 
title="<?php the_title(); ?>"><?php the_title(); ?></a> 

is not showing the title of the class but the title of the teacher plus I'm not sure it fully works.

  • Just use get_permalink and get_the_title instead, both functions allows you to pass the post ID as a parameter. In this case you want to pass the post ID of the class (you need to echo both functions as it returns a value). – gdaniel Jul 2 '15 at 14:12
  • Still not working. How do I get the ID of the class that has the current teacher added to it? Problem is I'm getting the title of the inner loop although I"m trying to get the title of the outer loop. – Raul Jul 2 '15 at 14:21
  • According to your loop the ID should be $cust_loop->ID . Do a print_r($cust_loop) to see if you find classes IDs in there. – gdaniel Jul 2 '15 at 14:24
  • I am getting ids in it but then I use $cust_loop->ID I'm still getting the name of the teacher 5 times. Maybe I'm not putting it correctly. Maybe the info is there but I don't know how to make use of it inside my code. – Raul Jul 2 '15 at 14:50
  • Did you try echo get_the_title($cust_loop->ID); ? – gdaniel Jul 2 '15 at 14:53
2

After hours and hours I finally figured it out. I only had to replace wp_reset_postdata(); with $cust_loop->reset_postdata();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.