Skip Navigation

[Resolved] Select the parent and child when creating a relationship

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

Problem:

I have a form for connecting posts to create a relationship.

1) I can set the parent but need help setting the child.

2) If the relationship already exists to not show the form by URL parameters.

Solution:

1) You can try attribute "child_item" like this:

[cred-relationship-form ... child_item='123']

2) It needs custom codes, for example:

https://toolset.com/forums/topic/select-the-parent-and-child-when-creating-a-relationship/#post-1367057

Relevant Documentation:

This support ticket is created 4 years, 6 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 15 replies, has 2 voices.

Last updated by Mario 4 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#1362875
lc-toolset38398.PNG

1. I have a form for connecting posts to create a relationship.
I can set the parent but need help setting the child.

[toolset_access role="seeker" operator="allow"] 
[cred-relationship-form form='seeker-bookmark-to-care-provider' parent_item='$current' child_item='[wpv-view name="view-bmk-seek-connect-2"]']
[/toolset_access]

2. If the relationship already exists to not show the form.

#1362897

This is a one to many relationship.

#1363077

Hello,

Q1) I can set the parent but need help setting the child.
I assume we are talking about the attribute "child_item" of shortcode [cred-relationship-form] in your codes, the attribute "child_item" supports only one post ID, it does not support post IDs, for example you can use specific post ID as the value, like this:

[cred-relationship-form ...  child_item='123']

Q2) If the relationship already exists to not show the form.
You can use [wpv-conditional] shortcode to check if there isn't existed related post, then display the relationship form, for example:

[wpv-conditional if="( '[wpv-post-id item="@RELATIONSHIP-SLUG.child"]' eq '' )"]
Here display the relationship form
[/wpv-conditional]

Please replace "RELATIONSHIP-SLUG" with that post type relationship slug.

More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional
https://toolset.com/documentation/user-guides/views-shortcodes/item-attribute/

#1363793

edit

#1363829

Hi Yang,

[cred-relationship-form ... child_item='6386']

Thanks that works (hard coded id)

how can I populate the child_item with a post_id pasted from the url parameter?

#1363939

You can get the URL parameter value with shortcode [wpv-search-term], and use it to populate the child_item attribute value, for example:

[cred-relationship-form ... child_item='[wpv-search-term param="MY-URL-PARAM"]']

Please replace "MY-URL-PARAM" with your URL parameter name.

More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-search-term

#1364037

Hi Yang,

Inserted code and does not pick it up.
Place [wpv-search-term] above the code to see what is displays and nothing is output.

[wpv-search-term]

[toolset_access role="seeker" operator="allow"]
[cred-relationship-form form='seeker-bookmark-to-care-provider' parent_item='$current' child_item='[wpv-search-term param="post_ids"]']
[/toolset_access]

url : /?kjak=3737993&post_ids=6386&namId=Jack1&

#1364065

I have tried it in my localhost, it works fine.

Please check these in your website:
1) Make sure you are using the latest version of Toolset plugins, you can download them here:
https://toolset.com/account/downloads/

2) In case it is a compatibility problem, please deactivate all other plugins, and switch to wordpress default theme 2019, deactivate all custom PHP/JS code snippets, and test again

4) If the problem still persists, please provide database dump file(ZIP file) of your website, also point out the problem page URL and form URL, I need to test and debug it in my localhost, thanks
https://toolset.com/faq/provide-supporters-copy-site/

#1365145

Thanks for the details, I have done below modifications in your website:
Edit the post view "view-bmk-seek-connect-3":
hidden link
in section "Loop item in view-bmk-seek-connect-3", line 4 edit the codes to:

[cred-relationship-form form='seeker-bookmark-to-care-provider' parent_item='$current'  child_item='[wpv-search-term param="post_ids"]']

Please test again, check if it is fixed, thanks

#1365461

Hi Yang

a). Thank you for the assistance that worked can I ask what you did as I can not find any change in the code I used.

b). Checking to see if a relationship already exists between two posts

[wpv-conditional if="( '[wpv-post-id item='@cbookmark.child']' eq '' )"]
Here display the relationship form inside 3
[/wpv-conditional]

I have place the code you provided inside "view-bmk-seek-connect-3" as this finds the parent "Seeker" .
When tested against a saved relationship or against a post where no relationship exists it does not return any result.

#1365949

Q1) can I ask what you did as I can not find any change in the code I used.
You were using HTML encoded characters, something like this:

[cred-relationship-form form='seeker-bookmark-to-care-provider' parent_item='$current'  child_item=& #39;[wpv-search-term param="post_ids"]& #39;]

The string & #39; should be just a single quote '

Q2) b). Checking to see if a relationship already exists between two posts

Please edit the post view "view-bmk-seek-connect-3"
hidden link
In section "Loop item in view-bmk-seek-connect-3", you are using below codes:

[wpv-conditional if="( '[wpv-post-id item='@cbookmark.child']' eq ' ' )"]
Here display the relationship form inside 3a
[/wpv-conditional]

There is an extra blank space between those two single quotes, it should be:

[wpv-conditional if="( '[wpv-post-id item='@cbookmark.child']' eq '' )"]
Here display the relationship form inside 3a
[/wpv-conditional]

It might be a copy/paste problem, please let me know if you need assistance to fix the problem.

#1366857

Hi Yang,

No the code does work. (placed in "view-bmk-seek-connect-3")
The code returns the same results wea ther the relationship exists or not.

I tested on the account I have given you.

There are two cp-providers

1. Jack1 - there is no relationship
2. Helen - relationship exists

#1367057

Thanks for the details, I have checked it again in your website, you are using many-to-many relationship "cbookmark", which is different from what you mentioned above:
https://toolset.com/forums/topic/select-the-parent-and-child-when-creating-a-relationship/#post-1362897

This is a one to many relationship.

You can check the relationship settings in your website:
hidden link

So it is different, and needs custom codes, I have done below modifications in your website:
1) Dashboard-> Toolset-> Settings-> Custom codes, create a new snippet "check-relationship", withe below codes:

add_shortcode('check-relationship', function($atts, $content){
	$seekers_id = get_the_ID();
	$cp_providers_id = 0;
  	if(isset($_GET['post_ids'])){
      $cp_providers_id = $_GET['post_ids'];
    }
  	$res = 0;
    $query = new WP_Query( 
     array(
      'post_type' => 'cp-provider',
      'posts_per_page' => -1,
      //new toolset_relationships query argument
      'toolset_relationships' => array(
       'role' => 'child',
       'related_to' => $seekers_id,
       // this will work only with relationships that have existed before the migration
       // if possible, use the relationship slug instead of an array
       'relationship' => 'cbookmark'
      ),
      'fields' => 'ids',
     )
    );
    $ids = $query->posts;
  	if(in_array($cp_providers_id, $ids)){
    	$res = 1;
    }
  	return $res;
});

It will create a new custom shortcode [check-relationship]

2) Dashboard-> Toolset-> Settings-> front-end-content
in section "Third-party shortcode arguments", add above shortcode name: check-relationship

3) Edit the post view "view-bmk-seek-connect-3":
hidden link

in section "Loop item in view-bmk-seek-connect-3", change the codes as below:

[wpv-conditional if="( '[check-relationship]' eq 1 )"]
    relationship exist
[/wpv-conditional]

[wpv-conditional if="( '[check-relationship]' eq '1' )" evaluate="false"]
    relationship does not exist
[/wpv-conditional]

Please test again, check if it is what you want.

More help:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

#1369253

Yang that worked perfectly.

I am trying to delete the relationship using this code :

[cred-delete-relationship role_items='$fromViews' relationship='cbookmark' redirect='self']Delete this relationship[/cred-delete-relationship]

It does not show the delete button. I have updated the access control seekers, authors and cbookmarks but still not showing.

The above code has been placed with the post view "view-bmk-seek-connect-3":

As this finds both the posts and checks if the relationship exists.

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/i-am-trying-to-delete-the-relationship-using-this-code/

#1369541

I assume the original question of this thread is resolved, for the new question, please check the new thread here:
https://toolset.com/forums/topic/i-am-trying-to-delete-the-relationship-using-this-code/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.