WordPress.org

Ready to get started?Download WordPress

Forums

CPT-onomies: Using Custom Post Types as Taxonomies
[resolved] Display information from the cptonomy term (6 posts)

  1. TiagoPires
    Member
    Posted 1 year ago #

    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?

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

  2. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    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?

  3. TiagoPires
    Member
    Posted 1 year ago #

    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 :P

  4. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    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();
          the_title();
          the_post_thumbnail();
       endwhile;
    endif;
  5. TiagoPires
    Member
    Posted 1 year ago #

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

  6. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    Good deal! You're welcome!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic