Views is a WordPress plugin that lets you display post relationships.
In our user guides, you can find more information on how to display child posts, related posts, brother pages, fields of parents and fields of grandparents.
When you ask for help or report issues, make sure to tell us the type of the relationship you are trying to build and the structure of your data.
Viewing 15 topics - 151 through 165 (of 292 total)
Problem: I have two post types in a one-to-many relationship. I would like to create a custom search View of the child post type, filtering by distance based on an address field in the parent post type, and include other filters based on custom fields in the child post type.
Solution: A View of post type A cannot filter by fields in post type B, so you would have to copy the parent address field into the child posts automatically using custom code, then filter by the address field in the child post type.
Problem: I have a one-to-many post relationship between two custom post types. In the template for the child post type, I would like to display "Next" and "Previous" links to paginate between the sibling posts.
Solution: Use a custom shortcode like this example that implements the post relationships API to create pagination links:
// https://toolset.com/forums/topic/next-and-previous-sibling-posts-in-a-one-to-many-post-relationship/
// get a link to the next or previous sibling post in child post content template
// example: [tssupp-next-prev-child current="[wpv-post-id]" step="1" relslug="book-chapter"][/tssupp-next-prev-child]
// current: current post id, step: 1 for next or -1 for previous, relslug: slug of the post relationship
function tssupp_next_prev_child($atts) {
$a = shortcode_atts( array(
'current' => 0,
'step' => '1',
'relslug' => '',
), $atts );
$link = '';
// get all child posts
$relationship_slug = $a['relslug'];
$current_child_id = $a['current'];
$parent_id = toolset_get_related_post( $current_child_id, $relationship_slug );
$sibling_args = array(
'query_by_role'=>'parent',
'limit'=>1000,
'role_to_return'=>'child',
'orderby'=>'title'
);
$siblings = toolset_get_related_posts( $parent_id, $relationship_slug, $sibling_args );
// loop over child posts and get index of the current post
foreach($siblings as $i=>$sibling) {
if( $sibling == $current_child_id ) {
break;
}
}
// increment or decrement index for next or previous sibling
$i += $a['step'];
// create link to next/previous sibling
if(isset($siblings[$i])){
$perm = get_the_permalink( $siblings[$i] );
$title = get_the_title( $siblings[$i] );
$link .= $a['step']=='1' ? "Next: " : "Previous: ";
$link .= "<a href='" . $perm . "'>" . $title . "</a>";
}
// output the link, or empty string if not set
return $link;
}
add_shortcode( 'tssupp-next-prev-child', 'tssupp_next_prev_child' );
You'll use the shortcode like this to generate a "Next" link (if necessary):
Problem: I would like to know whether I should use a Post Reference field or a Post Relationship, and understand the differences between the two.
Solution: A post reference field is a very simple version of a one-to-many (O2M) relationship. In the database, they are handled identically. The main differences are how they are managed in wp-admin. For example, with a O2M relationship between Clients and Vehicles, you can edit the parent Client post and see all the child Vehicle posts easily in the post relationship editor panel. If you want to disconnect one of the Vehicle posts, you can do it from the Client post. However, with a post reference field, you cannot easily manage the related Vehicles from the Client post. You would have to edit each Vehicle post to change the post reference Client. So if you want more management capabilities in wp-admin, you should use a O2M relationship. If you do not need this type of management, you can use a Post Reference field instead.
If you need the ability to create multiple Client post references in the same Vehicle post, you should use a M2M relationship instead of a repeating post reference field.
Problem:
The user would like to display fields in a repeatable field group of a related post.
Solution:
Repeatable groups need to be displayed using a view. And because you want to display the repeatable group from a related post you will need to create a content template too. The content template will be used to switch the context from the current post(bonus) to the related post(casino), and the view needs to be inserted inside this content template and hold a query filter on the relationship of the repeatable group. Does it make sense?
You will be able to display the content template by using the item attribute the same way it is used when you want to display a regular custom field.