One to many & many to many post relationships

Support MB Relationships One to many & many to many post relationshipsResolved

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #11146
    @mindspark@mindspark
    Participant

    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:

    1. 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.
    2. 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.

    #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();
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.