Support » Plugin: CPT-onomies: Using Custom Post Types as Taxonomies » Display information from the cptonomy term

  • Resolved TiagoPires


    in the scenario where ‘songs’ are linked to ‘albums’ where ‘albums’ generate the terms (‘albums’ are used as a cptonomy). The song ‘Freddie the Freeloader’ is linked to the album ‘kind of blue’. Which arguments can we use to get_posts() to fetch (for instance) the featured image of the album ‘kind of blue’ to show in the ‘freddie the freeloader’ song single post or within the song archive loop?

    Anyone had figure this one out?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Rachel Cherry


    Let me make sure I understand what you’re trying to do.

    “albums” is the cpt-onomy attached to “songs”, so “songs” is the parent in this scenario. So when you’re displaying a “song”, you want to pull which “albums” have assigned themselves to the “song” and then display the album’s featured image?

    The “song” edit screen has a list of albums on the side as “cptonomies” so a “song” get’s linked to one or more albums.

    In the single-song.php Id like to show the “album” title and thumbnail on the side.

    I hope I make any sense 😛

    Plugin Author Rachel Cherry


    Basically, you need to get the object’s (song’s) term (albums) IDs and then retrieve albums info but only the albums that are “attached” to the song.

    This little snippet of code should help, adjusting parameters to fit your needs:

    // retrieve the IDs of albums "attached" to this song
    $albums = wp_get_object_terms( get_the_ID(), 'albums', array( 'fields' => 'ids' ) );
    // use "the Loop" to retrieve album info, but we only want the albums with particular IDs ('post__in')
    $albums = new WP_Query( array( 'post_type' => 'albums', 'post__in' => $albums, 'orderby' => 'title', 'order' => 'ASC', 'showposts' => -1 ) );
    // print the albums
    if ( $albums->have_posts() ) :
       while( $albums->have_posts() ) : $albums->the_post();

    Hi Rachael, Thanks for showing the way to get the CPTerm data. It works great!

    Plugin Author Rachel Cherry


    Good deal! You’re welcome!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display information from the cptonomy term’ is closed to new replies.