0

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".

enter image description here

$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.

@JacobPeattie When I look in the database to see the relationships I was able to find this:

enter image description here

So 36 is the parent Service Category post_id and 43 is the child Service post_id in the post_meta table. I was able to find the relationship I just have never done a meta_query()

2
  • This depends on how Toolset's "Post Relationship" field works, so you should consult with them. But your code assumes that the value selected there is saved as the post_parent of the post. I don't think this is a safe assumption, as posts typically cannot have a parent of a different post type. It seems likely to me that that this relationship would be saved as meta, so instead of querying post_parent in $loop2, you probably need to be doing a meta query for the custom field value. Apr 9, 2018 at 15:20
  • @JacobPeattie I should, but I'm using the free version so they prolly won't help much. I left on update on the question to answer how the relationship is being stored.
    – Tom Bird
    Apr 9, 2018 at 15:25

2 Answers 2

0

So I was finally able to find what I needed:

                $loop2 = new WP_Query([
                  'post_type' => 'service',
                  'numberposts' => -1,
                  'meta_key' => '_wpcf_belongs_service-category_id',
                  'toolset_relationships' => [
                    'role' => 'child',
                    'related_to' => $parent_id,
                    'relationship' => ['service-category', 'service']
                  ]
                ]);

https://wp-types.com/documentation/customizing-sites-using-php/displaying-child-posts/

0

Based on what you've posted, we can see that the Service -> Service Category relationship is saved as post meta. So what you want to do is query posts that have the _wpcf_belongs_service-category_id meta value that matches the ID of the Service Category you're looking for.

So instead of querying post_parent, which is looking at the post_parent column of the posts table, not meta, you need to add a meta query. The documentation for that is here, but for your example you want to change

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

To this:

$loop2 = new WP_Query([
    'post_type' => 'service', 
    'meta_key' => '_wpcf_belongs_service-category_id',
    'meta_value' => get_the_ID(),
]);
3
  • I think you're mostly right, the only other part was that you had to add the toolset_relationships part. If you update your answer with that I delete my answer and mark yours correct!
    – Tom Bird
    Apr 9, 2018 at 15:48
  • That shouldn't be required based on the way the data is stored, but toolset_relationships is likely provided by the plugin to facilitate queries on more complex relationships (and allow the developers to change how data is stored without affecting existing code). Apr 9, 2018 at 15:50
  • Ok I'll try running that without and see if it works
    – Tom Bird
    Apr 12, 2018 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.