Show all child posts from parent in CPT single template

Support MB Relationships Show all child posts from parent in CPT single templateResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #29025
    ahmedahmed
    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?

    #29042
    Long NguyenLong 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
    ahmedahmed
    Participant

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

    #29075
    Long NguyenLong 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
    ahmedahmed
    Participant

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

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