Hi Toolset,
So I have 2 post types in relationship. Post Type A (parent) and B (child).
I want to list all post from post type A that only have children in Post type B.
Is it possible fix in PHP?
Thanks for help 🙂
Hi,
Thank you for contacting us and I'd be happy to assist.
To achieve this you can register a custom shortcode that can get the related child post's ID and returns 1 if it exists and 0 in case there is no child.
For example:
add_shortcode('check-child-exists', 'check_child_exists_func');
function check_child_exists_func() {
$relationship_slug = "book-shop-book";
$child_id = do_shortcode('[wpv-post-id item="@'.$relationship_slug.'.child"]');
if(!empty($child_id)) {
return 1;
}
else
{
return 0;
}
}
Note: You'll replace "book-shop-book" with the actual slug of your post-relationship.
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.
Also, add "check-child-exists" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.
After that, this shortcode will be available in the conditional block and you can wrap your view's loop content in a condition to only show when the value of this shortcode is equal to '1'.
( ref: https://toolset.com/course-lesson/using-toolset-conditional-block/ )
Example screenshot: hidden link
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar