Skip Navigation

[Resolved] Automatically activating a one-to-many relationship on custom post child

This support ticket is created 3 years, 2 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.

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: Africa/Casablanca (GMT+01:00)

This topic contains 15 replies, has 2 voices.

Last updated by nealM 3 years, 1 month ago.

Assisted by: Jamal.

Author
Posts
#1960403

Tell us what you are trying to do? Link child posts by populating the parent relationship field automatically with a value that is already part of the current post (a taxonomie used by CPT-onomies to make the relationship) We need to use a Toolset relationship

Is there any documentation that you are following? https://toolset.com/forums/topic/automatically-selecting-the-post-to-add-a-rfg-to-in-front-end-form/

Is there a similar example that we can see? hidden link

What is the link to your site? hidden link < staging copy

#1961451

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

I am not really sure to fully understand your request, can you provide more details? Maybe a concrete example?

In the meantime, you can always hook into the saving process of the post and use the Toolset Relationship API to connect posts. For example, using the toolset_connect_posts function.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

If you are saving posts using Toolset Forms, you can use Forms actions such as cred_save_data
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

If you are saving posts from the backend, you can use the default WordPress actions such as the wave_post action
https://developer.wordpress.org/reference/hooks/save_post/

#1961561

We have two custom posts : «Numéro» and «Commentaires de Lecture». Numéro is parent to many Commentaires de Lecture. We built the website nuitblanche.com (up and running) years ago with CPT-onomies (using custom post types as taxonomies) but we now need to replace all the links using Types relationships. CPT-onomies is no longer supported, for one thing...

There are over 5000 « Commentaires de Lectures » linke to 120 « Numéros ».

Populating [wpv-post-link item="@numero-commentaire-de-lecture.parent"] with the taxonomie field same value found in [tb-dynamic provider="__current_post" post="current" source="post-taxonomies-rich" field="numeros" separator=", " format="name" show="name" order="asc" originalContent="" formatted="Numéro 160"] looks like a good idea! But I don't see how...
hidden link

If I use the toolset_connect_posts function, can I add php to the form with adding a function in functions.php?
// Connect a custom Author post with the ID of "5" to a custom Book post with the ID of "7" in a post relationship between Books and Authors
toolset_connect_posts( 'book-author', 5, 7 );
How would I go about doing this? Should I replace 5 (parent) and 7 (child) with the above shortcodes?

#1961643

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

You said Numéro is parent to many Commentaires de Lecture Is this a Toolset relationship? Or is it from CPT-onomies perspective?

How would I go about doing this? Should I replace 5 (parent) and 7 (child) with the above shortcodes?
No, you can't use shortcodes inside custom code. Shortcodes are meant to be used inside the WordPress editors, not inside custom code. But you can use wpv_do_shortcode or do_shortcode functions to use shortcodes inside custom code, for example:

$link = wpv_do_shortcode('[wpv-post-link item="@numero-commentaire-de-lecture.parent"]');
// or
$link = do_shortcode('[wpv-post-link item="@numero-commentaire-de-lecture.parent"]');

wpv_do_shortcode is a Toolset function that allows to use nested shortcodes.

I am not familiar with how the CPT-onomies plugin stores data. I can't really suggest what to do with the existing data.
Please note that this will involve custom code, which is out of the scope of the Toolset support. If you are not comfortable with PHP programming, I suggest hiring a developer for this matter. You can check the list of our partners

#1961741

YES, this a Toolset relationship. I have the taxonomie field readily available with the value I need to populate each toolset relationship parent field, so why is it so hard to replace it?
This article mentions how to replace a value with a string. this is exactly what I want to do but I'm not getting a result.
hidden link.
The article says :
Add the field that creates a dropdown to set the parent post
When reating the child post form you can insert a field that creates a select box to select the parent post. Here you simply need to add a value that preselects the parent: value='[wpv-post-id id="$current_page"]'

[cred_field field='@an-pattern-review.parent' class='form-control' output='bootstrap' select_text='--- nicht eingestellt ---' value='[wpv-post-id id="$current_page"]']

It should work 🙂

#1961833

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

I think that we get to a point where I need a concrete example to help you further. Something like:
1. I open the post xxx
2. The post has the details yyy.
3. etc.
4. etc.
5. I expect to get the post xxx child/parent of post zzz.

Otherwise, I am very confused about your use case. Please explain a concrete example in small steps like above.

#1961961

We have two custom posts : «Numéro» and «Commentaires de Lecture». «Numéro» is PARENT to «Commentaires de Lecture» posts.
In the example I sent you, the article «Les patriarches du zen» is a «Commentaire de Lecture» custom post type.
It is a CHILD of post «Numéro 98».
Here is the CHILD is hidden link
Here is the PARENT hidden link

I want to link them with a Toolset Relationships. So the proper "one-to-many relationship" was created and it is working: I can manually select the correct Numéro for each CHILD in the backend editor for a given «Commentaires de Lecture». But I have over 5000 to link! I want to avoid having to link each post manually.

So I created a frontend form with Toolset forms and displayed it in each CHILD post so I could select the PARENT from the frontend. But it would be easier it the PARENT field was already populated with the value "Numéro 98" before the form is submitted.

I want to fill in the PARENT dropdown with value «Numéro 98». I have shortcode that will produce the data but I don't know how to modify the PARENT field dropdown with this value.

THANKS!

#1962655

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

If I understood correctly you want to set the parent post automatically in the Toolset form based on your shortcode, right?
If that's the case, please allow me temporary access to your website and let me know where the shortcode is defined, I would like to review it. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1962953

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Thank you for the credentials. I see the picture more clearly.

So, you are using the shortcode generated from a Single Field to get the term of this CPT-onomie taxonomy(Numeros). Unfotunately this cannot get the term's ID. According to the CPT-onomies plugin's page, the terms hold the same ID as the post(used as taxonomy). And that's what we need to automatically select on the Toolset form on the parent field.

I tried the custom code on the following thread to get the term's ID, but it does not work for me. Maybe the CPT-onomies plugin does name the taxonomy somehow I don't know.
https://toolset.com/forums/topic/how-do-i-show-a-taxonomy-id-instead-of-the-name/

If you can come up with a shortcode that will return the ID of the post "Numéro 161", we can use it inside the form so we have the parent post automatically selected. Does it make sense?

#1963987

Makes a lot of sense. That's exactly my problem. I'll try to find that shortcode. Thanks.

#1964045

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Awesome. Thank you!
I'll set this ticket as waiting for your reply, which should keep it open for 3 weeks. You will receive an email notification before the ticket is closed.

#1968883

I still need help. I'm searching for more information. The original relationships were created ny CPT-onomies. In order to get the post id of the parent (I can display the path and the slug, but I don't see how to dispolay the parent post id).
I will have to learn about CPT-onomies unique functions. hidden link. If anyone has an answer as to how I can make a shortcode to display a parent post id in a child template using CPT-onomies, that would be great.

#1968939

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

According to the plugin's page:
- The CPT-onomy term’s term ID is the same as the post ID.
- What’s really great about CPT-onomies is that they function just like any other taxonomy, allowing you to use WordPress taxonomy functions, like get_terms(), get_the_terms() and wp_get_object_terms(), to access the CPT-onomy information you need.

This should be enough to get the post ID that represents the CPT-onomy term ID. I tried on your website with Toolset shortcodes, which are based on WordPress functions, to no avail. I can't troubleshoot it longer because it is considered custom code and is therefore out of the scope of the support. Consider hiring a developer or one of our partners https://toolset.com/contractors/

As you are able to get the CPT-onomy term slug, I invite you to try a custom shortcode that will use get_term_by function to get the term ID. According to the plugin's documentation, that term ID is the same as the post ID.
Once you get this shortcode done, you just use it in the value attribute of the Toolset Form field.

#1971371

Hello

[dsense_term_id] is the shortcode the contractor has created (in the Toolset custom codes). This shortcode contains/displays the POST ID. I want to set the parent Form Field with this value.
The value is set in this example but I can't submit and the link is not active.
hidden link
You said : Once you get this shortcode done, you just use it in the value attribute of the Toolset Form field.
How can I do this?

#1972021

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

You can add the shortcode in the value attribute of the relationship field. ... value="[the-shortcode-here]" ...
Something like:

[cred_field field='@country-capital-city.parent' value="[the-shortcode-here]" class='form-control' output='bootstrap' select_text='--- not set ---' required='false']

I wanted to check it on your website but the credentials are not working for me anymore. But, it seems that you have already made it, right? hidden link

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