Support

Account

Home Forums ACF PRO Display CF from different CPT into the loop

Solved

Display CF from different CPT into the loop

  • Hello,

    I have two CPT, artists and videos. In the videos cpt I am using the relation custom field to relate each video to a given artist and oEmbed custom field to display the youtube videos.

    Now, I want to display the videos inside the artist CPT, in a given section. I am using this code but it doesn’t work becouse it didn’t get the video oEmbed and the video title:

    <div class="container">
    	<div class="row">
    
    		<?php while ( have_posts() ) : the_post(); ?>
    		<?php 
    		/*
    		*  Query posts for a relationship value.
    		*  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
    		*/
    
    		$videos = get_posts(array(
    			'post_type' => 'video',
    			'meta_query' => array(
    				array(
    							'key' => 'artista_del_video', // name of custom field
    							'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    							'compare' => 'LIKE'
    							)
    				)
    			));
    
    			?>
    			<?php if( $videos ): ?>
    			<div class="col-sm-12"><h2 class="seccion">Fotos</h2></div>
    			<?php foreach( $videos as $video ): ?>
    			<article class="post blog-post">
                    <div class="video-container"><?php the_field('url_del_video'); ?></div>
                    <h3 class="titulo-video"><?php the_title(); ?></h3>
                  </article>
    		<?php endforeach; ?>
    	<?php endif; ?>
    
    <?php endwhile; // end of the loop. ?>
    
    </div><!-- #row -->
    </div><!-- #container -->
  • Hi @kevin-mamaqi

    You’re almost there. Change your foreach loop to this so it sets up the post object properly and allows you to use the_field and wordpress functions.

    
    <?php foreach( $videos as $post ): setup_postdata($post); ?>
    	<article class="post blog-post">
    		<div class="video-container"><?php the_field('url_del_video'); ?></div>
    		<h3 class="titulo-video"><?php the_title(); ?></h3>
    	</article>
    <?php endforeach; wp_reset_postdata(); ?>
    
  • This reply has been marked as private.
  • This reply has been marked as private.
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Display CF from different CPT into the loop’ is closed to new replies.