Relationship with Custom Post Types
-
Hello!
Great plugin! Thanks for the time and thought you have put towards this. It’s appreciated by me and the community.
I could use a little bit of help. I have two custom post types created with Magic Fields 2. One is Discography, and one is Videos.
I’m trying to display only the videos that relate to the album. Right now, The album will show, and then ALL videos will show, not only the connected ones, but all the videos I have uploaded.
I include the code I have. I have a hunch the nested post loop is causing an issue, but I’m not even sure if I set this up correctly. Maybe I don’t have to declare the post type again? The primary post type is discography, and secondary is Videos.
<div class="dyContainer"> <div class="album_container"> <h3>Albums</h3> <div class="col1"> <?php $args = array( 'post_type' => 'discography' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); ?> <div class="album_wrap"> <?php the_post_thumbnail(); ?> <h4><?php the_title(); ?></h4> <span class="year"><?php echo get('year'); ?></span> </div> <?php endwhile; ?> </div> <a href="#" class="itunes_link"><span class="itunes_icon"></span>see all albums on iTunes</a> </div> <div class="album_content"> <h3>tracklist + Videos</h3> <span class="choose">select an album<br/> to learn more</span> <div class="col2"> <?php $args = array( 'post_type' => 'discography' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); ?> <div class="album hide"> <div class="track_wrap"> <h3><?php $title1 = the_title(); echo $title1; ?></h3><span class="year"><?php echo get('year'); ?></span> <div class="track_list"> <ol> <?php $tracks = get_group('track'); foreach($tracks as $track){ echo '<li><a>'; echo $track['track_title'][1]; echo '<span>' . $track['track_title_additional'][1] . '</span>'; echo '</a></li>'; } ?> </ol> </div><!-- end track list --> </div><!-- end track wrap --> <div class="videos_wrap"> <?php $my_query = new WP_Query( array( 'post_type' => 'discography' )); p2p_type( 'posts_to_posts' )->each_connected( $my_query, array(), 'video' ); while ( $my_query->have_posts() ) : $my_query->the_post(); $title2 = the_title(); if ($title1 == $title2) { echo "Here!"; echo $title2; // Display connected videos echo '<ul>'; foreach ( $post->video as $post ) : setup_postdata( $post ); echo '<li>' . the_title() . '</li>'; endforeach; echo '</ul>'; } wp_reset_postdata(); endwhile; ?> </div><!-- end video wrap --> </div> <? endwhile; ?> </div> </div><!-- end album content -->
Any help would be appreciated. Thanks!
- The topic ‘Relationship with Custom Post Types’ is closed to new replies.