WordPress trying to query two custom types to get child from the parent

enter image description here

So basically I have these service categories, under that there are the actual services. I am basically trying to loop through the categories and display category title, then underneath that I want to loop through all of the corresponding child items. I’m using “Toolset” custom field plugin with WordPress. I have the loop working for the “Service Categories” and now I am trying to loop through the “Service Categories” children custom post type of “Services”.

Codewise this is what I have right now:

<?php
            $loop = new WP_Query(['post_type' => 'service-category']);
            while ( $loop->have_posts() ) : $loop->the_post();
            ?>

                <h2 class="title">
                    <?php echo the_title(); ?>
                    <?php if(types_render_field('subtitle')) {?>
                      <span class="subtitle"><?php echo types_render_field('subtitle');?></span>
                    <?php }?>
                </h2>


                <?php
                $loop2 = new WP_Query(['post_type' => 'service', 'post_parent' => get_the_ID()]);
                ?>

                <?php while ( $loop2->have_posts() ) : $loop2->the_post();?>

                <h2 class="title">
                    <?php echo the_title(); ?>
                    <?php if(types_render_field('subtitle')) {?>
                      <span class="subtitle"><?php echo types_render_field('subtitle');?></span>
                    <?php }?>
                </h2>

                <?php endwhile;?>


          <?php endwhile; ?>

This is a screenshot of how I am associating the child custom post “Service” to the parent “Services-Category”.

$loop2 is where I am having the trouble trying to get the associated child items. How can I accomplish this? Any assistance would be greatly appreciated, I’ve burnt through two days searching for docs and all I find is the way to do it through the ui and not a template method.

Read more here:: WordPress trying to query two custom types to get child from the parent

Leave a Reply

Your email address will not be published. Required fields are marked *