Custom Fields
Re-Envisioned
Support MB Relationships Show all child posts from parent in CPT single template

  • Creator
    Topic
  • #29025
    Resolved ahmed
    Participant

    Hi, I have a issue-based magazine. Each issue contains articles. Accrodingly, I have 2 post types, i.e., Issue and Article (usually 15 articles in each issue).I have also set up a relationship from issues_to_articles.
    Now, I want to display in the single template of Issue, all related articles.
    How do I possibly achieve it?

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

    Hi,

    In the template of Issue post type, you can use the demo code to get the related Article posts

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

    Get more details here https://docs.metabox.io/extensions/mb-relationships/#posts

    #29059
    ahmed
    Participant

    I am completely a noob. Where do I put this code? In fuctions.php or MB Views template?

    #29075
    Long Nguyen
    Moderator

    Hi,

    You can put the code to the template file that shows the single post, contact your theme support to ask for the template file.

    If you want to use the View, please follow this documentation to know how to run PHP functions in View https://docs.metabox.io/extensions/mb-views/#running-php-functions

    #29098
    ahmed
    Participant

    Ok. I'll give it a try. Thank you for your prompt response.

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