One to many & many to many post relationships
Support › MB Relationships › One to many & many to many post relationshipsResolved
- This topic has 1 reply, 2 voices, and was last updated 7 years, 3 months ago by
Anh Tran.
-
AuthorPosts
-
August 28, 2018 at 8:18 AM #11146
@mindspark
ParticipantHi 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 don't want to have to recreate SPEAKERS and SPONSORS for every event. I want to add them once and then easily connect posts as needed.
- regarding the display - I am creating a SESSION page - much like you would expect to see on an EVENT page - where I need to show ALL of the info from the SESSION as well as the related posts - SPEAKER and SPONSOR.
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.
September 1, 2018 at 2:17 PM #11208Anh Tran
KeymasterHi 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(); -
AuthorPosts
- You must be logged in to reply to this topic.