Skip Navigation

[Resolved] I want to list all parent posts that only have children

This support ticket is created 3 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#1724321

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 🙂

#1725545

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.