0

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?!

2
  • 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
    Commented Feb 6, 2013 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 Commented Feb 6, 2013 at 17:11

2 Answers 2

0

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!

0
0

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();  ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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