You can tell Views the context of the post that you choose, using the id argument. This way, you control which item in the database is used when processing fields and Content Templates.
Types and Views shortcodes normally display fields of the current post. If you’re inserting them into a Content Template, you’ll get the field of the displayed post. If you’re inserting into a View, you’ll get the fields of the post in the loop.
But, there’s more. Types supports parent / child relationship between posts. This means that one post belongs to another. Let’s look at an example to make it clearer.
To include a field from the parent post, you would use the following shortcode:
[types field="xxxx" id="$yyyy"][/types]
Where xxxx is the field of the parent you want to show, and yyyy is the slug-name of the parent. The “$” is required, as this represents a variable where the parent post name will be replaced by the post ID, internally.
WordPress also supports hierarchy on some post types: for example, WordPress Pages can have a parent Page. If you enter $parent as the id, you will get data related to the WordPress parent:
[types field="xxxx" id="$parent"][/types]
Also, when you insert a View into a page, you might need to get some data from that page. To do so, you can set $current_page as the id attribute value:
[types field="xxxx" id="$current_page"][/types]
1. Rooms, belonging to Houses
Let’s say that we’re building a site for houses. Houses have rooms, so a Room would be the child of House.
In your Room Content Template you could add:
This house is located at [types field="address" id="$house"].
This tells Views to get the parent House “address” custom field and display it.
When we show a room, we might want to link back to the House parent post that the Room child post belongs to. To do this, we’ll need to add a Views shortcode to the Room Content Template.
This tells Views that you’re inserting the field for the House parent item. Views will lookup the House parent and produce the link (with title) for that item, not the Room.
To make this magic happen, the names must match exactly. When you say that a House is the parent of a Room, Types automatically adds a custom field called _wpcf_belongs_house_id to every Room. Then, Views will look for that custom field, load the House post according to the ID and use that to render custom fields.
2. Display the title of the WordPress parent of a page
Let’s say that you want to display the title of the current page’s parent. Like:
<quote> This page is part of our <em>real estate tutorial</em>. </quote>
To produce this, we’ll insert the following shortcode into the Content Template of the child page:
This will output the title, but of the page parent, instead of the current page.
3. Display the title of the current page
Why would you need anything special to display the title of the current page? You just need to use this, right?
Well, in some situations this might not be enough. Imagine you have a View displaying posts. You insert that View into a page and at some point inside that View loop you need to check some data from the current page. The shortcode above will not work, because it will return the title of the current post in the View loop. In this case, you need to use $current_page:
If you want to see a real example of those values in action, you can read how setting the parent ID allows us to display brother pages.