Support Forum
Support › MB Relationships › One to many & many to many post relationshipsResolved
Hi Anh. I'm starting to get the hang of things!
I need help relating a post to multiple other posts. See below:
SESSION should relate to both SPEAKER and SPONSOR. I've tried updating the examples you provide in the KB but can't figure out what I'm doing wrong.
And before I go any further, please note - there are 2 reasons I want to do this:
I think #2 is more complex and requires a hint from you to set things up.
I prefer to use your shortcode, but I don't know how to edit so that it pulls cutom fields from more than one post record.
hopefully this makes sense. let me know if you need any additional info. thx.
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();