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:
[wpv-view name='your-view-slug' placereference='[engineer-place-ids]']
Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
https://toolset.com/documentation/user-guides/passing-arguments-to-views/
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 12 replies, has 2 voices.
Last updated by 6 years, 5 months ago.
Assisted by: Christian Cox.