Custom Fields
Re-Envisioned
Support MB Views Displaying 2 custom post types via a relationship custom post type

  • Creator
    Topic
  • #26611
    Resolved Henri
    Participant

    Hi,

    I have Posts (default Post type) and Sources (custom Post type).
    A Post can have multiple Sources and a Source can be used for multiple Posts (many to many relationship).
    In the relationship I want to describe in a field why they are related. Because I can't add custom fields to a MB relationship, I thought to create another Custom Post type depicting the relationship between a Post and a Source: Post2Source. This way I can add a custom field to this Relationship. I've created 2 MB relationships for this:

    -Post-Post2Source: this is the 1 to many relationship from Post to Post2Source
    -Source-Post2Source: this is the 1 to many relationship from Source to Post2Source (a Post2Source record can only have 1 Source record)

    I've set this up and it holds all content and relationships.

    Now I want to create a MB View that displays a Post with its Sources. So I have to traverse the Post to the Sources via the Post2Source. I managed to display the Post content with the Post2Source content with the following code:

    <main>
        <article>
            <h2>
                {{ post.title }}
            </h2>
            {{ post.content }}
        </article>
        <section class="sources">
            {% set relationship = attribute( relationships, 'post-post2source' ) %}
            {% for post in relationship.to %}
                {{ post.title }}
                <p>
                    {{ post.content }}
                </p>
            {% endfor %}
        </section>
    </main>

    How do I retrieve the content of the Source? So in the existing "for loop" above, how can I query in the current retrieve 'Post-Post2Source' the related Source content? I tried to add another "for loop" using the relationship "Source-Post2Source", but that doesn't work. Apparently the focus is kept on the Post and doesn't change the focus to Post2Source, so it doesnt give any result.

    Is it possible in MB views to go from the Post to Post2Source to Source content in a query?

    Kind regards, Henri

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

    Hi,

    It's the multiple relationships as on this documentation https://docs.metabox.io/extensions/mb-relationships/#multiple-relationships.

    You can just create a PHP function to show the content as you want then use it on View with the proxy mb. mb.your_php_function().

    #26630
    Henri
    Participant

    Hi Long,

    so I can't do it just in the template tab on the MB View Edit View page on the backend of WordPress?

    Can you point me to some beginner articles how to create php functions for Metabox implementation? I'm really new at coding and I'm kinda in the dark. So if you have some great blogposts of Metabox you can point me to, I would be very grateful 🙂

    Kind regards, Henri

    #26687
    Henri
    Participant

    Hi Long,

    I've created the custom posts types, like described in the MB relationship article: https://docs.metabox.io/extensions/mb-relationships/#multiple-relationships

    I've added the 3 custom post types: artist, event, band and I've connected them with MB relationships. I've added a function in the child function.php theme as follows:

    function relatedposts(){
        $query = new WP_Query( [
        'relationship' => [
            'relation' => 'OR',
            [
                'id'   => 'events_to_bands',
                'from' => get_the_ID(),
            ],
            [
                'id'   => 'events_to_artists',
                'from' => get_the_ID(),
            ],
        ],
        'nopaging'     => true,
    ] );
    while ( $query->have_posts() ) {
        $query->the_post();
        echo get_the_title() . '<br>';
    }
    wp_reset_postdata();
    }

    Now I'm trying to call this in my MB View 'Artist' like so:
    {% mb.relatedposts() %}

    Nothing shows. I do have 1 artist connected with one event and the event is connected to 1 band. So when I'm displaying an artist, I want to show the event he is connected to and the bands that are connected to that event. But somehow the above code doesn't work. Can you help me?

    Kind regards, henri

    #26695
    Long Nguyen
    Moderator

    Hi Henri,

    It is impossible to show the connection from Artist -> Event -> Band, you can show one connection like this Artist -> Event <- Band.

    The code above should apply to the Event post type, then Artist and Band connect to this event will show up.

    #26821
    Long Nguyen
    Moderator

    Hi Henri,

    I've dived into this case and found a way to achieve this goal. We can use a query nested in the main query to get the connected post A > B > C as your expectation.

    I've created a post type book and have two relationships. Here is the code:

    add_action( 'mb_relationships_init', function() {
        MB_Relationships_API::register( [
            'id'    => 'posts_to_pages',
            'from'  => 'post',
            'to'    => 'page',
        ] );
    } );
    
    add_action( 'mb_relationships_init', function() {
        MB_Relationships_API::register( [
            'id'    => 'posts_to_books',
            'from'  => 'post',
            'to'    => 'book',
        ] );
    } );

    When displaying a page, I can show posts related to this page and books related to those posts

    $connected_post = new WP_Query( [
        'relationship' => [
            'id'   => 'posts_to_pages',
            'to' => get_the_ID(), // You can pass object ID or full object
        ],
        'nopaging'     => true,
    ] );
    while ( $connected_post->have_posts() ) : $connected_post->the_post();
        ?>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <?php 
            $connected_book = new WP_Query( [
                'relationship' => [
                    'id'   => 'posts_to_books',
                    'from' => $connected_post->post->ID, // You can pass object ID or full object
                ],
                'nopaging'     => true,
            ] );
            while ( $connected_book->have_posts() ) : $connected_book->the_post();
                ?>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <?php
            endwhile;
            $connected_book->reset_postdata();
    
         ?>
        <br>
        <?php
    endwhile;
    wp_reset_postdata();

    Let me know if it helped.

    #26835
    Henri
    Participant

    Hi Long,

    Thanks for researching this. It worked 🙂

    I've copied your code to the function.php file in the child theme. Is it also possible to do this in the MB view editor? Or do you really need this php code? In other words, can this be translated to the MB View editor syntax?

    Anyway, awesome that you solved this and I'll definitely keep this code example close, so I can use it in my projects. Kudos for the great support you are delivering 🙂

    Henri

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