• So, here is the story: i made a product catalogue with 3 custom post types:

    - Product
        - Filegroup (Child of Product)
            - File (Child of Filegroup)

    When on product page it should show a list with the Filegroup title and the loose files under the correct Filegroup. But it shows the children at both parents

    It’s like this now:

    - Filegroup 1
        - File 3 (child of Filegroup 2)
        - File 1 (child of Filegroup 1)
        - File 2 (child of Filegroup 1)
    
    - Filegroup 2
        - File 3 (child of Filegroup 2)
        - File 1 (child of Filegroup 1)
        - File 2 (child of Filegroup 1)

    And it needs to be:

    - Filegroup 1
        - File 1 (child of Filegroup 1)
        - File 2 (child of Filegroup 1)
    
    - Filegroup 2
        - File 3 (child of Filegroup 2)

    My current script

    <?php
        $childargs = array(
        'post_type' => 'filegroup',
        'numberposts' => -1,
        'order' => 'ASC',
        );
        $child_posts = get_posts($childargs);
    
        foreach ($child_posts as $child_post){
        $parentid = $child_post->ID;
            ?>
                <ul>
                    <li><strong><?php echo $child_post->post_title; ?></strong></li>
                        <?php
                            $childargs2 = array(
                            'post_type' => 'file',
                            'numberposts' => -1,
                            'post_parent' => '0'
                            );
                            $child_posts2 = get_posts($childargs2);
    
                            foreach ($child_posts2 as $child_post){
                            ?>
                                <li><?php echo $child_post->post_title; ?></li>
                            <?php
                            }
                        ?>
                </ul>
    
            <?php
        }
    ?>
  • The topic ‘Custom Post Type children and grand-children in one list’ is closed to new replies.