Support

Account

Home Forums ACF PRO How to get related posts from post object field

Helping

How to get related posts from post object field

  • Couldn’t figure this for weeks. Two custom post types. One has taxonomies. The other one is a post object of the first one.
    I want to show all custom post types from the second one, BUT from the taxonomy which is under the first one.

    Example:

    Artist CPT: Title = Mike. (taxonomy – > location, term – > europe )

    Event Title (relation field/post object )
    ————————————————-

    Event Lorem Ipsum 1 (relation field/post object “Mike”)
    Event Lorem Ipsum 2 (relation field/post object “Mike”, “Robbie”)
    Event Lorem Ipsum 3 (relation field/post object “Mike” , “Andrea”)
    Event Lorem Ipsum 4 (relation field/post object “Mike”, “Max”, “Antonio”)

    Now I want to show all the latest published events in general from the taxonomy location, term = europe.
    Also Inside each event, i want to show an artist title which here would be Mike.
    So event would have an event title, date, and artists related to that event.

  • This is the closest I got to what I want, but it isn’t the proper way. I need to be able to enter any post per page. And, the current way depends on the artists. And BCS of it, post per page doesn’t work properly.

    <?php 
                    $relation = new WP_Query( array(
    		            'tax_query' => array( 
    							array(
    								'taxonomy' => 'continent',
    								'field'    => 'term_id',
    								'terms'    => 836,
    							)
    						), 
     						'post_type'         => 'artist',
    					'posts_per_page'	=>  -1 ,
    
    				) );
    
             
    				if( $relation->have_posts() ) : 
    
    				while ( $relation->have_posts() ) : $relation->the_post(); ?>	
    
    					<?php
    
    					$featured_posts = get_field('relation');
    
    					if( $featured_posts && is_array($featured_posts) ):
    					
    				
    					$new_featured_posts = get_posts( array(
    						  'post_type'        => 'event',
    			              'post__in' => $featured_posts,
    			              'posts_per_page' =>4,
    						  'order' => 'DESC',
    						  'orderby' => 'meta_value',
    						  'meta_key' => 'webinar_date',
    					) );
    					?>
     
    				    <ul>
    						<?php
    					    foreach($new_featured_posts as $post){
    					    	?>
    					    		<li>
    						             <?php the_title(); ?> 
    									<p>
    										<?php the_field( 'event_date' ); ?>
    									</p>
    						        </li>
    					    	<?php
    					    }
    			
    			         ?>
    				    </ul>				    
    					
    				    <?php 
    				    // Reset the global post object so that the rest of the page works correctly.
    				    wp_reset_postdata(); ?>
    
    				<?php endif; ?>
     
    			<?php endwhile;  ?>
     
    			<?php endif;  ?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.