Skip Navigation

[Resolved] Displaying Siblings of a Child CPT on the Child Post

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
#2481525

I am revisiting a problem I had solved previously at https://toolset.com/forums/topic/displaying-sibling-posts-of-a-child-post-in-blocks/

I have a one-to-many relationship between a Parent CPT and Child CPT. I have Content Templates for both. Using a View Block, it is easy to show all Children on the Parent CPT, and the Parent on a Child CPT. However what I would like to do is show all 'Siblings' on a Child CPT.

Using the latest versions of Toolset (Blocks workflow), step 3.3 on the above link does not enable the radio buttons and prompts to create a view. I cannot seem to get this to work as it did before.

#2482341

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 show the related sibling posts on the child post's template, here is a simpler alternative.

1. In the template for the child post, you can create a view that shows the child post type. At this point, this view will be showing all the posts and not just the related siblings.

2. Next, you can use a custom function attached to the "wpv_filter_query" filter ( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query ), that gets the ID of the related parent post and then adds a relationship query filter, so that only the posts related to that parent are shown by the view:

For example, if the ID of your view is "12345" and the relations slug is "company-branch", the code will look like this:
(you'll change these two values as per your website)


add_filter('wpv_filter_query', function($query_args, $settings, $view_id){
	// get parent post's ID
	$parent = do_shortcode("[wpv-post-id item='@company-branch.parent']");
	// if specific view and if parent post ID exists
	if ( ((isset($settings['view_id']) && $settings['view_id'] == 12345)) && (!empty($parent)) ) {
		// query by related parent's connections
		$query_args['toolset_relationships'][] = array(
			'role' => 'child',
			'related_to' => $parent,
			'relationship' => 'company-branch'
		);
	}
	return $query_args;
}, 99, 3);

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 the active theme's "functions.php" file.

I hope this helps and please let me know if you need any further assistance with this.

regards,
Waqar

#2482609

Thank-you, this solution was well explained and worked as expected.

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