WordPress.org

Ready to get started?Download WordPress

Forums

CPT-onomies: Using Custom Post Types as Taxonomies
[resolved] CPTonomie terms according to custom taxonomy terms (4 posts)

  1. r1987
    Member
    Posted 1 year ago #

    Hello!

    This is a bit of a puzzle for me & I'm even doubting this is even possible in a simple way.

    Anyhow:

    1. I have two CPT called Performers and Productions.
    2. Performers also have an custom taxonomy called "Performer Field" with terms "Actor" "Dancer" "Musician".
    3. Performers are attached to Productions. Where I just select the names.
    4.How to display the Performers outputed by their field on a single production page?

    Like
    Actors: Tom Hanks, John Smith
    Dancers: Mississipi Group, New York Dancers
    . . .

    http://wordpress.org/extend/plugins/cpt-onomies/

  2. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    Ooh! A challenge! Here's what I got. I hope it helps:

    if ( have_posts() ) : while ( have_posts() ) : the_post();
    
       // first, figure out which performers are tied to this production. you only need the IDs.
       $performers = wp_get_object_terms( get_the_ID(), 'performers', array( 'fields' => 'ids' ) );
    
       // then, get all the performer field terms
       $performer_fields = get_terms( 'performer_field' );
    
       // loop through each field
       foreach( $performer_fields as $field ) {
    
          // save performer data for printing
          $print_performers = array();
    
          // figure out which posts are tied to this specific field
          $field_objects = get_objects_in_term( $field->term_id, $field->taxonomy );
    
          // by comparing to our array of performer IDs, we can figure out which ones belong to this production AND to this specific taxonomy
          // if matches both logic, save for printing later
          foreach( $field_objects as $performer_id ) {
             if ( in_array( $performer_id, $performers ) )
                $print_performers[] = '<a href="' . get_permalink( $performer_id ) . '">' . get_the_title( $performer_id ) . '</a>';
          }
    
          // print list
          if ( $print_performers ) {
             ?><p><?php echo $field->name . ': ' . implode( ', ', $print_performers ); ?></p><?php
          }
    
       }
    
    endwhile; endif;
  3. r1987
    Member
    Posted 1 year ago #

    Wow Rachel!

    What a puzzle indeed!
    This is fantastic. As I already thought that may-be i should create extra cptonomies for these fields also and dump the traxonomies.

    Thanks a big time!

  4. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    You're welcome!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic