• Resolved nyrngrs24

    (@nyrngrs24)


    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!

    http://wordpress.org/extend/plugins/posts-to-posts/

Viewing 1 replies (of 1 total)
  • Thread Starter nyrngrs24

    (@nyrngrs24)

    Welp, i got it working! I was confused. I was using too many post loops. It clicked the other night. I’m supposed to use the existing post loop to show related videos.

    Thanks again for spending the time on this plugin and the great docs.

    here is the new code if people want to see:

    <section id="music" class="section clearfix">
    	<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="https://itunes.apple.com/artist/eve/id108709" 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">
    						<h3><?php $title1 = the_title();
    							echo $title1; ?><span class="year"><?php echo get('year'); ?></span></h3>
    						<div class="track_wrap">
    							<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 -->
    							<a href="#" class="itunes_link"><span class="itunes_icon"></span>see this album on iTunes</a>
    						</div><!-- end track wrap -->
    
    						<div class="videos_wrap">
    							<div class="videos">
    								<?php
    								// Find connected videos
    								$connected = new WP_Query( array(
    									'connected_type' => 'posts_to_posts',
    									'connected_items' => $post,
    									'nopaging' => true
    								) );
    								// Display connected videos
    
    								echo '<ul>';
    								while ( $connected->have_posts() ) : $connected->the_post();
    									echo '<li><a rel="video" href="' . get('youtube_link') . '">';
    									echo the_post_thumbnail('video');
    									echo '</a></li>';
    								endwhile;
    								echo '</ul>';
    
    								wp_reset_postdata(); // set $post back to original post
    								?>
    
    							</div>
    							<a target="_blank" class="youtube" href="https://www.youtube.com/user/EveVEVO">visit Eve's Youtube channel</a>
    						</div><!-- end video wrap -->
    					</div>
    				<?php endwhile; ?>
    			</div>
    		</div><!-- end album content -->
    	</div>
    </section>
Viewing 1 replies (of 1 total)
  • The topic ‘Relationship with Custom Post Types’ is closed to new replies.