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.
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
Thank-you, this solution was well explained and worked as expected.