display relationships to each post in list of cpt

Support MB Relationships display relationships to each post in list of cpt

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #38942
    jimimacjimimac
    Participant

    Hi,

    I'm trying to display a list of custom post types with the title of any related posts next to it in the list.

    The list of cpts has an id of 'bookings' and the related posts have an id of 'contacts'.

    here's my code but no related posts are displaying...

    	$loop = new WP_Query(
        	array(
            	'post_type' => 'bookings'
       		)
    	);
    
    	while ( $loop->have_posts() ) : $loop->the_post();
    
    		echo the_title();
    		echo '<br/>';
    
    		$contacts = MB_Relationships_API::get_connected( [
    			'id'   => 'bookings_to_contacts',
    			'from' => get_the_ID(),
    		] );
    		foreach ( $contacts as $c ) {
    			echo $c->post_title;
    		}			
    
    	endwhile;
    	wp_reset_postdata();
    
    #38948
    PeterPeter
    Moderator

    Hello Jimimac,

    A similar code works as well on my local site

    $loop = new WP_Query(
    	array(
        	'post_type' => 'page',
        	'posts_per_page' => 5,
        	'order' => 'ASC'
    		)
    );
    while ( $loop->have_posts() ) : $loop->the_post();
    	echo the_title();
    	echo '<br/>';
    	$contacts = MB_Relationships_API::get_connected( [
    		'id'   => 'page-to-post',
    		'from' => get_the_ID(),
    	] );
    	foreach ( $contacts as $c ) {
    		echo $c->post_title;
    	}			
    endwhile;
    wp_reset_postdata();

    so please recheck the relationship ID and the from side when registering the relationship on your site.

    #38953
    jimimacjimimac
    Participant

    Ah it’s not working for me. Is there a way to check the relationship ID and from side anywhere?

    #38958
    PeterPeter
    Moderator

    You can check the relationship ID and from, to side when you create the relationship. Please share some screenshots of the relationship, I will help you to find them.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.