Custom Fields
Re-Envisioned
Support MB Views Traversing multiple related CPTs

  • Creator
    Topic
  • #32085
    Resolved anjo65
    Participant

    Hello,

    I created the following CPTs and relationships: Trip > TripDay > Event; Trip to TripDay 1:m, TripDay to Event 1:m.

    I am trying to create a query in MB Views that retrieves and displays data from a Trip, its associated TripDays and all events associated with each TripDay.

    With the help of the following support topic https://metabox.io/support/topic/displaying-2-custom-post-types-via-a-relationship-custom-post-type/ I was able to create a working writing PHP code (see below).

    <?php the_title(); ?>
    
    <?php
        $connected_tripday = new WP_Query( [
            'relationship' => [
                'id'   => 'relationship-trip-to-tripday',
                'from' => get_the_ID(),
            ],
            'nopaging'     => true,
        ] );
        while ( $connected_tripday->have_posts() ) : $connected_tripday->the_post();
            the_title(); 
                $connected_event = new WP_Query( [
                    'relationship' => [
                        'id'   => 'relationship-trip-day-to-event',
                        'from' => get_the_ID(),
                    ],
                    'nopaging'     => true,
                ] );
                while ( $connected_event->have_posts() ) : $connected_event->the_post();
                    the_title(); 
                endwhile;
                $connected_event->wp_reset_postdata();
        endwhile;
        wp_reset_postdata();
    ?>

    However I would much rather accomplish this in MB Views but have been unable to so far. I am able to retrieve TripDays related to a trip but not Events related to a TripDay.
    How can I accomplish this use case in MB Views?

    Thank you in advance.
    Andy

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #32105
    Long Nguyen
    Moderator

    Hi,

    Here is an example code to show tripday and event from the single trip view. Screenshot https://share.getcloudapp.com/7KuAn6We

    <h1>Trip: {{ post.title }}</h1>
    
    {% set tripday_args = { relationship: { id: 'relationship-trip-to-tripday', from: post.ID }, nopaging: true, post_type: 'tripday' } %}
    {% set tripdays = mb.get_posts( tripday_args ) %}
    <ul>
        {% for tripday in tripdays %}
            <li>Tripday: {{ tripday.post_title }}</li>
            {% set event_args = { relationship: { id: 'relationship-trip-day-to-event', from: tripday.ID }, nopaging: true, post_type: 'event' } %}
            {% set events = mb.get_posts( event_args ) %}
            <ul>
                {% for event in events %}
                    <li>Event: {{ event.post_title }}</li>
                {% endfor %}
            </ul>
        {% endfor %}    
    </ul>

    Refer to this topic https://metabox.io/support/topic/query-in-relationship-post-per-page/

    #32108
    anjo65
    Participant

    Hi Long,
    Thank you. The provided code works perfectly.
    Andy

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.