Support MB Relationships One to many & many to many post relationships Reply To: One to many & many to many post relationships

#11208
Anh TranAnh Tran
Keymaster

Hi Neil,

I fixed the snippet in the KB. In this case, I think it can be done with this code:

add_action( 'mb_relationships_init', function() {
    MB_Relationships_API::register( array(
        'id'   => 'sessions_to_speakers',
        'from' => 'session',
        'to'   => 'speaker',
    ) );
    MB_Relationships_API::register( array(
        'id'   => 'sessions_to_sponsors',
        'from' => 'session',
        'to'   => 'sponsor',
    ) );
} );

To show the related information (speakers, sponsors) when displaying sessions, please try this code:

// Display speakers
$speaker_query = new WP_Query( array(
    'relationship' => array(
        'id'   => 'sessions_to_speakers',
        'from' => get_the_ID(),
    ),
    'nopaging'     => true,
) );
while ( $speaker_query->have_posts() ) : $speaker_query->the_post();
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php
endwhile;
wp_reset_postdata();

// Display sponsors
$sponsor_query = new WP_Query( array(
    'relationship' => array(
        'id'   => 'sessions_to_sponsors',
        'from' => get_the_ID(),
    ),
    'nopaging'     => true,
) );
while ( $sponsor_query->have_posts() ) : $sponsor_query->the_post();
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php
endwhile;
wp_reset_postdata();