Skip Navigation

[Resolved] Create view of brother posts using the current post’s parent

This thread is resolved. Here is a description of the problem and solution.

Problem: I have two custom post types in a one-to-many relationship. On the child post type single post page, I would like to show a list of brother (sibling) posts - i.e. all other posts of the child custom post type that have the same parent post. I have implemented a "Post parent" filter, but it doesn't seem to work as expected.

Solution: The "Post parent" filter isn't intended for use in one-to-many relationships between two different post types. It is only intended for use in a hierarchical post type, like the native WordPress "Pages" post type. Instead, you should use the "Post relationship" filter, and use a shortcode attribute to supply the post parent ID:

[wpv-view name="your-styles-view-slug" wpvprchildof="[wpv-post-id id='$development']"]

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-id
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

This support ticket is created 6 years, 11 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by paulG-5 6 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#596028
Screen Shot 2017-12-05 at 15.39.55.png

Hi.

I've trying to create a view similar to the real estate theme but with a difference.

I have a parent post (Developments) with Child posts (Styles)
I'm trying to create a view to be show on the Style page to show all the other styles (brothers) within that Development (Parent)

Having created the main view, I've set the 'Content Selection on Post Type: Style and set a Query Filter to Post parent filer: Parent is the current post in the loop.

I then created a content template as follows: [wpv-view name="related-styles"]

And then created another content template: [wpv-post-body id="$parent" view_template="related-styles-in-style-page"]

I get zero results back.

If I set the post parent filter to No parent, it will return every post in Styles.

I'm confused as I've replicated something I've made on another site that works okay.

Also, is there a better way of doing this other than creating one View and 2 Content Templates? Seems a bit silly.

#596086
Screen Shot 2017-12-05 at 3.23.29 PM.png

Filter to Post parent filer: Parent is the current post in the loop.
The Post Parent filter is only intended for use within a single hierarchical post type, like native WordPress Pages. It doesn't apply to parent / child relationships between different post types. You should use a Post Relationship filter instead, set to display posts that are a child of a particular parent ID, passed into the View using a shortcode attribute "wpvprchildof". See the attached screenshot for an example of this setup. This technique is called passing arguments into Views, and it allows you to use the same View on all your Style pages - the parent Development ID is variable, determined by the context of the current Style. If that sounded like a foreign language, don't worry yet.

Assuming the Development post type slug is "development", on the Style page you can determine the current Style's parent Development ID with the wpv-post-id shortcode and the $parent id operator:

[wpv-post-id id='$development']

Knowing that, you can pass that development ID into your View of Styles shortcode attribute like so:

[wpv-view name="your-styles-view-slug" wpvprchildof="[wpv-post-id id='$development']"]

Now your View's filter will show all Styles that are children of the current Style's parent Development. Make sure you check the setting "Don't include the current post in the results" so you don't see the current Style listed here as well.

More about the wpv-post-id shortcode:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-id

More about passing arguments into Views here:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

#596405
Screen Shot 2017-12-06 at 17.48.42.png
Screen Shot 2017-12-06 at 17.39.52.png
Screen Shot 2017-12-06 at 17.40.29.png

Hi Christian.
Thank you for this. It does make sense and I followed this to the tee, however it still displays all the styles rather than just the styles that are only in this development.
Also I get a warning in the filter "This will filter out posts of the following types, because they are not hierarchical: Styles"

However Style is a child of development.
Not sure what I have done wrong here.
3 screenshots attached.

#596449

Right, as I mentioned before you don't want to use a post parent filter. Notice that just above the warning it says "Post parent filter" in the top of the filter box? This indicates a Post parent filter instead of a Post relationship filter. I would delete this post parent filter, and then add a new filter based on "Post Relationship" instead.

#596703

Ah! Sorry I obviously didn't read that correctly. I understand it now, and it works. Thanks so much.