Problem: I have two post types in a one-to-many post relationship. Artists are the parents, and Paintings are the children. In the Artists post type, I have a checkbox custom field. I would like to create a View of Artist posts that only shows Artists with related Paintings. I would also like to include any Artist where the checkbox is checked.
Solution: To accomplish this without custom code, you need 3 Views:
- #1: View of Artists, filtered by checkbox field. Use the legacy Views editor and choose the output type "List with separators". Output the Artists IDs as a comma-separated list.
- #2: View of Artists, filtered by post ID using a shortcode attribute "ids". Design the output as you would like to see on the front-end of the site.
- #3: View of Paintings with no filters. Use the legacy Views editor and choose the output type "List with separators". In the loop, include the post ID of the parent Artist post.
Insert View #2 in your template or page using a shortcode. In the ids attribute, place View #1 and View #3.
Problem: I have two post types - Hosts and Structures - in a post relationship. Each User can be the author of one Host post, and can create multiple Structure posts using Forms. When the User submits a Form to create one of these Structure posts, I would like to automatically connect it to the User's Host post.
Solution: Use the cred_save_data API to trigger custom code when the Structure post is created. Use the toolset_connect_posts API to link the two posts programmatically.
Problem: I have 3 custom post types - Artists, Books, and Works on Paper. I have two one-to-many (O2M) relationships: Artists-Books where Artist is parent and Book is child, and Artists-Works On Paper where Artist is parent and Work on Paper is child. I'm using the Favorites plugin to allow my site visitors to add favorite Books and favorite Works on Paper. In the list of favorite posts, I would like to display information about the related Artist.
Solution: The best way to handle this is to use the Favorites plugin's filters API to filter the favorites list HTML. You can use the Toolset Post Relationships API toolset_get_related_posts to get information from related posts in that filter callback:
/**
* Toolset Support
* Filter the Favorites list information to display related Artiste info
* Currently supports Livres and Oeuvres Sur Papiers post types for Favorites
* Extendable by adding a case statement for new post type / relationship slug
* https://toolset.com/forums/topic/find-related-custom-post-type/
*/
add_filter( 'favorites/list/listing/html', 'custom_favorites_listing_html', 10, 4 );
function custom_favorites_listing_html($html, $markup_template, $post_id, $list_options)
{
// relationship slugs per favorite post type
$post_type = get_post_type( $post_id );
switch($post_type) {
case "livre":
$rel_slug = "livre_artiste";
break;
case "oeuvre-sur-papier":
$rel_slug = "oeuvre-sur-papier-artiste";
break;
default:
return $html;
};
// Get related posts with Toolset post relationships API
$children_args = [
'query_by_role' => 'parent',
'role_to_return' => 'child'
];
$children = toolset_get_related_posts( $post_id, $rel_slug, $children_args);
// Build a string of related post titles, separated by a comma,
// and concatenate it into response HTML, then return it
$child_titles = [];
if( $children && count( $children ) > 0 ) {
foreach( $children as $child ){
$child_titles[]= get_the_title($child);
}
return $html . implode(',', $child_titles);
}
// in case no related posts are found, return the default HTML
return $html;
}
Problem: I would like to create a custom shortcode that displays the number of child posts for the current parent post. I'm referencing an older ticket but it does not seem to work as expected.
Solution: Instead of the _wpcf_belongs_slug_id postmeta key, you must use the new post relationships API with post relationships created in Types 3.0+. A custom shortcode solution is available.