Relationship between two Custom Post Types – all child records show up
-
I am building a website for a travelling company where I need a parent child relationship between a Trip and Packaged tours offered by the company to visit that place. Trip is parent and Packaged tours are children to it and this is an 1-many relationship.
To achieve this I created two Custom Post Types (Trip and Package-Tour). In Trip I added Package-Tour as a Relationship field.
Then I added two Package Tours (PT-A and PT-B) and two Trips (Trip-A and Trip-B). I associated PT-A with Trip-A and PT-B with Trip-B.
The problem is both PT-A and PT-B are visible in both trip pages while I was expecting only the associated one to come up.
In single.php I have:
<?php $packages = get_posts(array( 'post_type' => 'package_tour', 'meta_query' => array( 'key' => 'available_tour_packages', 'value' => '"' . get_the_ID() . '"', 'compare' => 'LIKE' ) )); if($packages) { foreach($packages as $package) { echo "<h1>" . $package->post_title . "</h1>"; echo "<p>" . $package->trip_itinerary . "</p>"; echo "<p>" . $package->booking_procedure . "</p>"; } } ?>
I tried by using
'compare' => 'IN'
but it did not change any behaviour.echo
-ing get_the_ID gives me correct ID of Trip post but in the database I saw that values saved under post_parent Package-Tour posts are all 0 (not the ID of Trip post), which mean that current relationship is probably not the true parent-child relationship – the way I thought.I have started WordPress very recently and don’t have much experience of it yet. What is the correct way of doing this?
- The topic ‘Relationship between two Custom Post Types – all child records show up’ is closed to new replies.