Skip Navigation

[Resolved] Set intermediary relationships in CRED new post form

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to establish relationships between two posts in a New Post Form.

Solution: It's not currently possible to manage M2M relationships in a Post Form, you must create a Relationship Form. To predefine the values, you can use the parent_item and child_item shortcode attributes in the cred_relationship_form shortcode:

[cred-relationship-form form='form-relationship-zoukers-events' parent_item='[wpv-post-id id="$current_page"]' child_item='[wpv-view name="get-zouker-id"]']

The View must return the correct Post ID for the child item, and must have no empty spaces in the loop editor markup:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-post-id]</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found][/wpv-no-items-found][wpv-layout-end]

Finally you must apply this custom filter that strips out additional markup from the View's results so it can be used as a shortcode attribute:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
 
function prefix_clean_view_output( $out, $id ) {
  $ids = array( 123 );
  if ( in_array( $id, $ids )) {
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

Relevant Documentation:
https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 9 replies, has 2 voices.

Last updated by tiagoS-3 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#953226
Screen Shot 2018-07-22 at 17.52.49.png

Hi!

I have a relationship between two CPTs: “Events” and “Dancer”.

These relationship have an intermediary CPT named “Stack”.



I have a form to create a new “Stack”.

The form have all Stack fields (created using auto-generate form), but don’t have fields to set the relationship, to choose an “Event” and “Dancer”.



How can i add fields in form, to set the relationship?

I need to set an “Event” and “Dancer” when user submit form to create new “Stack”.

---

 “add post fields” In form content editor, don’t load relationship type (attached image).

#953238

Hi, you will need a Relationship Form to establish many-to-many (M2M) relationships on the front-end of the site. Unfortunately it is not currently possible to create a new post (or edit an existing post) and establish M2M relationships in the same Form. More information about connecting M2M posts is available here: https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/

#953266

Nice, this solves partly.



I need assign users in events.

I trying to use this solution… Can you check if is the best way?



I create a CPT named “Dancer” and other “Event”.

Users have one “Dancer” post (like an author).

I create a relationship between “Dancers” and “Events”.



Now, I need a button (form with fields hidden) in event page, when logged-in user click, add a relationship between “Event” (getting in the page) and “Dancer” (logged in author).



Using Relationship Form I can load “Event” field.

“Dancer” field needs to start typing to show value.

My idea is to load booth fields in hidden mode.
When the user clicks “submit”, create the relationship.



#953667

Okay it sounds like you want to set the values for the parent_item and child_item in a cred_relationship_form shortcode. Something like this:

[cred-relationship-form form='connect-dancer-and-event' parent_item='123' child_item='456']

To make this dynamic, you need to know the Event ID and the Dancer ID. The Event ID can be determined from the current page, like this:

[wpv-post-id id="$current_page"]

The Dancer ID is not as easy to determine. My suggestion is to use a View. Create a View of Dancer posts, filtered by Author, where the Author is the current logged-in User. In the Loop Output, insert the wpv-post-id shortcode. Then in your child theme's functions.php file, add the following filter:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
  $ids = array( 123 );
  if ( in_array( $id, $ids )) {
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

Replace 123 with the numeric ID of this new View.

Now you can complete the cred_relationship_form shortcode with dynamic shortcode attributes like this:

[cred-relationship-form form='connect-dancer-and-event' parent_item='[wpv-post-id id="$current_page"]' child_item='[wpv-view name="your-dancer-view-slug"]']

Replace the Form slug and View slug as needed, and check the parent and child items. I wasn't sure from your description which is the child and which is the parent in the relationship, so these may be backwards in my example.

#953669

Oh and I forgot to explain how to hide the fields. I would just use basic CSS applied to a wrapper div, like this:

<div style="display:none;">
        <div class="form-group">
		<label>Event</label>
		[cred-relationship-role role="parent"]
	</div>
	<div class="form-group">
		<label>Dancer</label>
		[cred-relationship-role role="child"]
	</div>
</div>
#953857

Nice.

The “Dancer” ID I can get only using the view, I think don’t need the custom filter in functions.php. When I insert the view to get the “Dancer” id, return value correctly (check “180” in attachment).



But, when I insert view code in child_item, form become broken, returning “This relationship form no longer exists“.



The code bellow return image attached:



——

[wpv-view name='get-zouker-id']
//return “Dancer” ID correctly

[cred-relationship-form form="form-relationship-zoukers-events" parent_item='[wpv-post-id id="$current_page"]' child_item="180" ]
//load Event ID corretly and the child if set id directly (without view)


[cred-relationship-form form='connect-dancer-and-event' parent_item='[wpv-post-id id="$current_page"]' child_item='[wpv-view name="get-zouker-id"]']
//broken when set view inside child_item
——



Is it a syntax error?

#953875

A View will produce extra markup, like div tags, spaces, and HTML comments. Even though it looks right on the front-end of the site, the markup source is not formatted for the filter. You can inspect the results of a View to see this in the browser. The extra markup and spaces will break the filter, so the code in functions.php is required to strip that extra markup and create a clean output.

#954505
Screen Shot 2018-07-24 at 11.50.27.png

Ok. When I add the code in functions.php return no data in view inside the form.
Now we have code bellow returning the attached image:

----
[wpv-view name="get-zouker-id"]
//return “Dancer” ID correctly

[cred-relationship-form form="form-relationship-zoukers-events" parent_item='[wpv-post-id id="$current_page"]' child_item="180" ]
//load "Event" ID corretly and the child if set id directly (without view)


[cred-relationship-form form='form-relationship-zoukers-events' parent_item='[wpv-post-id id="$current_page"]' child_item='[wpv-view name="get-zouker-id"]']
// Don't load get-zouker-id view data
----

I expect to see the booth fields load dynamically.
parent_item (Event) is ok. Now we need child_item (Dancer).

When I remove functions.php code, the form has broken "This relationship form no longer exists".

#954586

Okay the next thing to try is to remove all the spaces from the get-zouker-id View's Loop, like this:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-post-id]</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found][/wpv-no-items-found][wpv-layout-end]

If this does not resolve the issue, I'll need to take a closer look. Please provide a URL where I can see the results of the get-zouker-id View.

#954600

Nice, works when I remove all spaces in view.

Thanks!!!!