Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hopefully I can explain this correctly, as I'm getting confused myself now with what I want to achieve!

I have two Custom Post Types, "Students" & "Boats"

In "students" I have the Taxonomy "Classes" and then Taxonomy Terms of; "Class of 2012", "Class of 2011" etc. etc.

In "boats" I have the Taxonomy "Projects" and then Taxonomy Terms of; "Class of 2012", "Class of 2011" etc. etc.

What I'd like to do is: On the 'Class of 2012' page for students, I currently have a list of all the students, that's no problem and it's working well. What I'd like is to also display all the boats from the "Class of 2012" boats taxonomy term on the same page.

That's where I'm having trouble. Ideally I'd like to get all the 'boats' in the 'Class of 2012' taxonomy by saying something like: get all posts from the boats post type in the taxonomy term of the same name as current taxonomy term.

Does that makes sense?!

share|improve this question
    
If the Taxonomies "Classes" and "Projects" have the same values, and you want to connect them, why don't you use the same Taxonomy for both Custom Post Types? –  fischi Feb 6 '13 at 17:06
    
There are other functions in place on the site that means they need to be different, I've chosen the one relationship that needs connecting in order to not confuse things further –  Adam Montague Feb 6 '13 at 17:11

2 Answers 2

As you are on the student's page, you can get the "Classes" Taxonomy Name really simple.

$classes = get_the_terms( get_the_ID(), 'Classes' );

Next step is to select the Class you want to use, I assume it is the first one listed.

$theclass = $classes['0']->name;

You can get the other Taxonomy like this:

$projectsterm = get_term_by( 'name', $theclass, 'Projects' );

This only works if both have the same term name! But now you just have to get the posts for the Boats:

$args = array(
    'post_type' => 'boats',
    'projects' => $projectsterm->slug
);
$boats = get_posts( $args );

Hope this works for you!

share|improve this answer

Thank's for your efforts but your answer wasn't working for me. I have found a solution for my problem, I'm no PHP master so I'm not sure if it's particularly 'clean' or the correct way to do things, but this works for me!...

<?php 
    $terms = get_the_terms( $post->id, 'classes' ); // get an array of all the terms as objects.
    $term_slug = array(); // save the slugs in an array
    $term_name = array(); // save the slugs in an array

    foreach( $terms as $term ) {
        $term_slug[] = $term->slug; // this grabs the hyphenated slug
        $term_name[] = $term->name; // this grabs the actual name
    }           
?>
<?php query_posts( array( 'post_type' => 'boats', 'projects' => $term_slug[0], 'orderby'=>'title','order'=>'ASC' ) );
  if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<li><a href="<?php echo home_url(); ?>?projects=<?php echo $term_slug[0]; ?>"><?php the_title(); ?></a></li>

<?php endwhile; endif; wp_reset_query();  ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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