Toolset lets you develop truly custom sites, including WooCommerce, Membership and Directory/Classifieds sites.
Visit our dedicated tutorials to learn how to build each of these sites using Toolset:
Problem: I would like to connect Users and Places in a M2M relationship. I have another post type Contact that can be associated with one Place. I would like to create a View of Contacts so each User can see a list of Contacts that are related to the same Places the User is related to.
Solution:
- Create a custom post type "Places" and add some Places posts.
- Create a custom post type to hold the Form submissions. I'll call it "Contacts" but you can call it whatever makes sense for you.
- Create a custom field group that holds all the custom Contact fields you want to collect in the Form. Use a post reference field to link this Contact to one Place.
- Create a "Create Contact" new post Form and place it on your site where visitors can access the Form.
- Create a proxy post type for Users, I'll call it "Engineers". Create Engineer posts for a few Users to test with.
- Set up a many-to-many (M2M) relationship between Engineers and Places. In my example the relationship slug is "engineer-place", Engineer is the parent and Place is the child.
- Connect some Engineers and some Places in wp-admin or with a Relationship Form. Note that you cannot connect Engineers and Places when adding or editing an Engineer or Place with Forms, a separate Relationship Form is required.
- Create a View of Contacts, filtered by Post Reference, where the post reference ID is set by a shortcode attribute. Use the "in" operator so you can pass an array of values into the shortcode.
- In the Loop of this View of Contacts, build the table shown in the Engineer portal. Skip the reply link and come back to that later.
- Place this View on the Engineer portal page. You will see all Contacts now, since we have not set the filter values yet.
- Create a custom shortcode that will return a comma-separated list of Place IDs, filtered by post relationship using the current User's Engineer ID. The goal is to produce a list of the Engineer's related Places like 12, 34, 56 so we can pass that into the View as a shortcode attribute "placereference". This is a general guide you can start with:
function get_related_place_ids_for_current_engineer( $atts )
{
global $current_user;
// get the current user's engineer proxy post
$engineer_args = array(
'post_type' => 'engineer',
'post_status' => 'publish',
'author' => $current_user->ID,
);
$engineer_posts = get_posts($engineer_args);
// there should be a proxy post, but check anyway and exit if not
if( !isset($engineer_posts[0]))
return;
$engineer = $engineer_posts[0];
// get all the places related to this engineer in m2m 'engineer-place'
$places = toolset_get_related_posts(
$engineer,
'engineer-place',
'parent',
1000000,
0,
array(),
'post_id',
'child'
);
return implode(', ', $places);
}
add_shortcode( 'engineer-place-ids', 'get_related_place_ids_for_current_engineer' );
- Modify the slugs and parent / child as needed.
- Register this shortcode in Toolset > Settings > Frontend Content > Third party shortcode arguments.
- Now add the custom shortcode to the View shortcode attribute filter:
I have created a Custom Post Type and a Content Template for these posts.
What if I need to show these posts through a second Content Template?
Let's say that I create a View, that only certain users can see, and when they click on each post title they can load this post, but they can see completely different fields and layout than what all other users see?
Get all fields value of custom repeatable field group using PHP codes.
Solution:
The Toolset repeatable field group is based on post types, you can consider it as one-to-many relationship, each item of repeatable field group is a post, so you can use Types API function toolset_get_related_posts() to get all related posts.
Then get the custom fields value of each related post with function types_render_field().