Custom Fields
Re-Envisioned
Support MB Relationships One to many & many to many post relationships

This topic contains 1 reply, has 2 voices, and was last updated by  Anh Tran 9 months, 2 weeks ago.

  • Creator
    Topic
  • #11146
    Resolved
    @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.

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #11208

    Anh 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 1 replies (of 1 total)

You must be logged in to reply to this topic.