Skip Navigation

[Resolved] Pass on a value from a parent post to a child post in CRED

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

Problem: I would like to create a link from a View of parent posts to a CRED form that creates child posts. The parent post should be automatically selected in the CRED form when the user clicks the child post link.

Solution: CRED offers a child post shortcode that can be placed in a View of parent posts that will automate this process.

[cred_child_link_form form='506' parent_id='-1' target='_self']

You can place this shortcode in a Content Template or directly into your View's Loop Output editor. If you want to place this code in a Content Template that is constructed with Beaver Builder, then you cannot use a button module. You should use an HTML module.

Relevant Documentation: https://toolset.com/documentation/user-guides/cred-forms-for-child-content/
https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/

This support ticket is created 7 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)

Author
Posts
#546549

I am trying to populate a CRED field with a value from a parent CPT.

I have a CRED field called 'Game' which is part of a child CPT. The CRED form is called from the Parent CPT which also has a data element called 'Game'. I want the value from 'Game' of the Parent post to be auto-filled in the CRED field 'Game' (and also to be stored in the child CPT of course).

Basically I am trying to pre-fill some fields in the CRED form so that a user does not have to type over values.

I already figured out how to display values in the CRED form by having created a function. But I cannot get that to work when trying to populate a field

Any ideas?

Thanks,
Michael

#546635

Hi there are a few different approaches here. You can use the "urlparam" or "value" attribute in the cred_field shortcode to predefine a value for the field, assuming you know what that value should be.
Hardcoded value:

[cred_field field="game" post="child-post-type-slug" value="1234"]

URL parameter value:

[cred_field field="game" post="child-post-type-slug" urlparam="parent_game"]

Then link to the CRED form page using the parentgame URL parameter:
hidden link

Another approach is to remove the game input field from your CRED form altogether and automatically set that value with the CRED API's cred_save_data hook. General idea:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==3456)
    {
        if (isset($_POST['_wpcf_belongs_parentslug_id']))
        {
            // add it to saved post meta
            $parent_game = get_post_meta($_POST['_wpcf_belongs_parentslug_id'], 'wpcf-game');
            add_post_meta($post_id, 'wpcf-game', $parent_game, true);
        }
    }
}

More information about this approach: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Let me know if any of these approaches will work for you and I can provide some additional assistance.

#546780

Hi Christian,

Thaks for you reply.

I am trying to use the URL parameter. I think this works best because the CRED form is evoked when a user clicks a button from a view (e.g. a view displaying multiple games and clicking a button - which is part of the view countent layout - to request a quote on travel expenses of visiting that game).

If I understand yopu correctly, I add [cred_field field="game" post="child-post-type-slug" urlparam="parent_game"] to the CRED form.

Next I need to link to the CRED form page using the parentgame URL parameter:
hidden link

My question is: where do I put "hidden link"?

Is that the link I need the buitton to navigate to? And how do I populate parent_post_id and parent_game dynamically since I am using a view?

Thanks,
Michael

#546914

Is that the link I need the buitton to navigate to?
Yes, this is the link your user should navigate to when they click the button.

how do I populate parent_post_id and parent_game dynamically
Parent post id will be the ID of the current page, and parent game will be the value of the game custom field for the current page. So you will build up a link URL using a couple of shortcodes:

<em><u>hidden link</u></em> name="game"]

Let me know how it goes.

#546925

Just to see if I understand you correctly and to explain my situaiton in a bit more detail:

I have a parent post that is called "Games". the Title of a post from Games is "team A versus team B".

I have a child post called "Quote". One of the fields there is "Game". I want the title from the Games post to be prefilled in the field "Game" of the CRED that creates a "Quote" post.

So the url you provided would than look like:
hidden link name="title"]

Also another question;
The link where the button goes to now reads:
[cred_child_link_form form='506' parent_id='-1' target='_self']. This link makes that I can display some values on the CRED form from the parent post. Will that still work when I replace it with the new link?

Thanks,
Michael

#546980

1. I want the value from 'Game' of the Parent post to be auto-filled in the CRED field 'Game' (and also to be stored in the child CPT of course).

2. I have a child post called "Quote". One of the fields there is "Game". I want the title from the Games post to be prefilled in the field "Game" of the CRED that creates a "Quote" post.

Okay, I am confused because initially you said that you want the value of the Game data element from the parent to be copied into the child post, so I assumed the parent post type has a custom field called "Game" which you want to copy over to the child post. Now it sounds like "Games" is the parent post type, and you want to copy the title of the Game post into the child Quote. These are completely different requests, unless I am misunderstanding you. Can you help me understand this a bit better?

If it's really #1, there's usually no need to copy the name of a parent post into a child post's custom post field, because you can always access parent post information from a child post. And the "Create child post link" option in CRED will automatically set the parent value for you, so no additional work is required here.

More information about parent post information here: https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/

#547072
-Blank UML - Page 1.png

Hi Christian,

Apologies for not being really clear. I have added an image / diagram that shows what I am trying to achieve. I hope this clarifies things. I do appreciate your feedback.

Thanks,
Michael

#547076

Ok thanks, this helps me understand better. You can disregard my earlier comments for now, because this is fairly straightforward with CRED and no additional code is necessary.

1. In your diagram the "I want to go" link will be a CRED child post link. The link URL will be automatically generated for you when you insert the link using the CRED Forms button.

2. In the CRED form to create a Quote, the input which allows your user to select the parent Game will automatically be set to the correct value if the user accesses the Quote CRED form using the "I want to go" link. This value is predetermined based on the URL parameters in the child post link.

3. When the Quote is submitted, the parent / child relationship to the desired Game will already be established. No additional work is needed here to add Game information to the Quote.

4. In your diagram, the "Game" information stored in a Quote is really just a reference to the parent Game in the Toolset system. So if you want to display information about the parent Game when you are displaying a Quote, you can access that information using the parent syntax described in the document I posted before: https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/

Let me know if you have any specific questions about this.

#547417

Hi Christian,

Thanks for your reply.

Unfortunatly, the "Parent Game" is not selected autmatically / set to the correct value even though I am using the created link for the CRED form.

So in the view i created the CRED link: [cred_child_link_form form='506' parent_id='-1' target='_self']
This is the link where the "I want to go" button refers to (which is the correct link because the Quote form opens and displays some of the data I want to display there so it links correctly. It only does not set the correct value for the Parent Game).
Am I missing something?

Thanks,
Michael

#547487

So in the view i created the CRED link: [cred_child_link_form form='506' parent_id='-1' target='_self']
The cred child link form shortcode must be placed inside the wpv-loop tags of your Loop Output editor. This looks okay, so can you share the entire Loop Output code here?

When you click the link, what is the full URL of the destination page?

If you place the actual ID of one of your parent posts in the URL manually and load the page, does the parent post get selected correctly? For example hidden link - replace 1234 with the ID of an actual parent post and press Enter.

#547572

Hi Christian,

I first tried to enter the ID manually. Unfortunately, this did not get the desired outcome. So here is the code from the entire loop output code (a grid with 2 columns):


[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop wrap="2" pad="true">
		[wpv-item index=1]
		<div class="row ">
			<div class="col-sm-6">[wpv-post-body view_template="Loop item in wedstrijden"]</div>
		[wpv-item index=other]
			<div class="col-sm-6">[wpv-post-body view_template="Loop item in wedstrijden"]</div>
		[wpv-item index=2]
			<div class="col-sm-6">[wpv-post-body view_template="Loop item in wedstrijden"]</div>
		</div>
		[wpv-item index=pad]
			<div class="col-sm-6"></div>
		[wpv-item index=pad-last]
			<div class="col-sm-6"></div>
    [cred_child_link_form form='506' parent_id='-1' target='_self']
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]Geen wedstrijden gevonden[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

The full url of the destination page =
hidden link

("wedstrijd" = Dutch for "Games", "offerte" = Dutch for "Quote")

Thanks,
Michael

#547583

ps: The input field at the bottom of the CRED (when you follow the link) is the Parent Game ("Wedstrijd Ouder") input field. Als you can see it reads: "Geen Ouder" which means "No Parent".
Of course you can select a Parent, but from a usability perspective it should of course be set to the selected game from the view.

#547586
Screen Shot 2017-07-11 at 3.25.33 PM.png

I first tried to enter the ID manually. Unfortunately, this did not get the desired outcome.
This URL seems to work correctly for me, can you take a look?
hidden link
"Arsenal - Leicester City" is selected automatically in the CRED form, corresponding to the ID 244. What am I misunderstanding?

It looks like you are using a Content Template to display each result. Please remove the cred child link shortcode from the Loop Output area. Create a new cred_child_link_form shortcode in the Content Template "Loop item in wedstrijden". Make sure you add some text, which will be displayed in the link. The page ID should be the page where you have your Quote CRED form, and you should select "Set the parent according to the currently displayed content". See the attached screenshot.

#547605

Hi Christian,

This is what I did:

- I followed your instructions. To do so I had to remove my Beaver Builder created Content Template. Adding the CRED form link worked just fine; when opening the Quote form it indeed automatically selects the correct parent game

- Using that exact same link with Beaver Builder to create my Content Template does not work.

I also do notice the following:

The link displayed in the browser when not using Beaver Builder for the Content Template =
hidden link

The link displayed in the browser when using Beaver Builder =
hidden link

I use a button moduele from Beaver Builder for this.
Than I tried just to add an HTML element and copy paste the CRED link as specified.
This also works fine.

So for now I think I am good. I just need to style the link as button in HTML to bypass the stuff Beaver Builder apparently does with links (or do you have any idea?)

#547616

I see, you won't be able to place the cred link shortcode in a button module because Beaver Builder expects to find only a simple URL in this field. The CRED link shortcode adds markup around the link, much like Beaver Builder does, so the BB button module doesn't work as expected. You can add CSS classes and additional options to the CRED link shortcode to help style it like a Beaver Builder button. If you show me a button on your site somewhere, I can provide some code that will modify your link to look like a BB button.