Skip Navigation

[Resolved] Use data from a parent post in a form

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 7 months, 1 week ago.

Assisted by: Nigel.

Author
Posts
#2692537

I have a CPT "Company" and a CPT "Delivery".
I have a relationship 1toM (company-delivery).

COMPANY has a few single checkboxes for some preferences.
I have the same checkboxes in the DELIVERY custom fields.

I'm using "Create Child Post Link" to open the page with the form to create a new DELIVERY post so the COMPANY is predefined in the form.

I would like to retrieve the preferences checkboxes values to use Javascript to pre-select the checkboxes of my form.

How can I achieve that ?

#2692610

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

This should be possible because when you use the Create Child Post Link it passes the ID of the parent post via a URL parameter, and you can use that to specify the source when outputting field values so that the values come from the parent post.

You don't need any JS, though you'll see that we run into a problem and will need to use PHP to register a custom shortcode.

If you have some checkbox fields in your child post form, you can set the default value to be that which we retrieve from the same field of the parent post.

The visual editor doesn't let you set a default value on a checkbox field, so you would need to switch to the Expert mode.

Locate the cred_field shortcode responsible for one of the checkbox fields. You can test having it checked by default by adding the attribute value="1" to it.

Now, we want the "1" to actually come from the value of the same field in the parent.

The URL parameter that passes the ID of the parent post should look something like parent_company_id.

We can reference the value of the URL parameter using the shortcode wpv-search-term, so this should generate the ID of the parent post:

[wpv-search-term param="parent_company_id"]

You can use this to provide the value for the item attribute which can be added to shortcodes to specify an alternate source for the data.

Assuming you have a checkbox field with a slug of "checkbox-one", then a shortcode like this would output the raw value of checkbox-one from the parent post:

[types field='checkbox-one' output='raw' item="[wpv-search-term param='parent_company_id']"][/types]

So you can use that to provide the default value for the corresponding checkbox field.

Putting that altogether would give you something like:

[cred_field field='confirm-one' force_type='field' class='form-check-input' output='bootstrap' value='[types field='checkbox-one' output='raw' item="[wpv-search-term param='parent_company_id']"][/types]']

Except, if you look closely you might appreciate that this is going to run into a problem because of the use of single and double quotes; because we essentially nest arguments up to 3 times, there is no combination of two types of quotes that is sufficient, we need three types of quotes to avoid breaking things, but we only have two.

We can resolve this by registering a custom shortcode to do the same as the wpv-search-term shortcode, but instead of declaring the parameter via a param shortcode attribute, we'll specify the attribute to use as shortcode content instead, e.g.

[url-param]parent_company_id[/url-param]

That way we get to specify the parameter without relying on quotes.

This is the code you need to register such a shortcode:

add_shortcode( 'url-param', function( $atts = [], $content = null ){

    $return = '';

    if ( isset( $_GET[$content]) ){
        $return = $_GET[$content];
    }

    return $return;    
});

(You can add that as a snippet at Toolset > Settings > Custom Code.)

To be able to use that shortcode where we need it, you need to register it at Toolset > Settings > Front-end content > Third-party shortcode arguments.

Finally, we can set the default value of the checkbox-one field based on the value from the parent post with

[cred_field field='checkbox-one' force_type='field' class='form-check-input' output='bootstrap' value="[types field='checkbox-one' output='raw' item='[url-param]parent_company_id[/url-param]'][/types]"]
#2692640

Hi Nigel.
Thanks for this explanation. However I stilled some assistance...

Should the code of the shortcode be exactly as you mentioned ? Because the "wpv-search-term param=" does not appear anywhere. How the system know that it has to apply that function ? If I understood well this code only take the content as is and so can contain " or ', right ?

Secondly, as the slugs of my CPT's are "company" and "delivery" and as there is a 1toM relationship between them (DELIVERIES are on the MANY side).
In the "Company" the field "td-content-movie" is the reference. So the field "deliver-movie" of the "delivery" should take the value of the "td-content-movie".
Is this part of the form correct then ? Because it does not work:

[cred_field field='deliver-movie' force_type='field' class='form-check-input' output='bootstrap' value="[types field='td-content-movie' output='raw' item='[url-param]parent_company_id[/url-param]'][/types]"]

#2692749

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry if I wasn't clear.

I was describing how we would ordinarily use the wpv-search-term shortcode to get the value of the parent post url parameter, but in this case we cannot, and so register a custom shortcode that does the same thing without using a shortcode attribute (param) that implies more switching of quotes than can be handled.

So we have a custom shortcode that will retrieve the URL parameter value, like so:

[url-param]parent_company_id[/url-param]

And so we can get the value of the checkbox custom field of the parent using the types shortcode and the item attribute to switch the context to the parent, with:

[types field='td-content-movie' output='raw' item="[url-param]parent_company_id[/url-param]"][/types]

And putting it together with the shortcode to output the form input gives:

[cred_field field='deliver-movie' force_type='field' class='form-check-input' output='bootstrap' value="[types field='td-content-movie' output='raw' item='[url-param]parent_company_id[/url-param]'][/types]"]

If the final part isn't working, try adding the url-param and types shortcodes shown above directly into the form markup and check the front end to see if they are successfully generating the expected values.

Double-check that the URL parameter generated actually is "parent_company_id".

Do those individual shortcodes generate the expected content themselves?

#2692771

"parent_company_id" is correct, so with that line it shows me the right name of the parent company.
[wpv-post-title output='raw' item="[wpv-search-term param='parent_company_id']"]

When I display [wpv-search-term param='parent_company_id'] or [url-param]parent_company_id[/url-param] both gives me the right ID.

However when I write that, that turns wrong.
[wpv-post-title output='raw' item="[url-param]parent_company_id[/url-param]"]

It's displaying the post title of the actual form (which is here "New Order") followed by a part of the code and gives:
New Orderparent_company_id[/url-param]"]

It's like it was not taking [url-param]parent_company_id[/url-param] as a value...

#2692773

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Did you do this?

>To be able to use that shortcode where we need it, you need to register it at Toolset > Settings > Front-end content > Third-party shortcode arguments.

#2692774

It works now. It. was a spelling mistake in the shortcode.
Thanks a lot for your help !