Skip Navigation

[Resolved] I want to populate fields in a CRED form (child CPT) from the parent CPT

This support ticket is created 7 years, 8 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
- 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+01:00)

This topic contains 14 replies, has 3 voices.

Last updated by peterv-6 7 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#426989

Parent = company-profile with a field "state" (Checkboxes)
child = announcement with a field "company-state" (checkboxes)

I want to enable the filtering of announcements by state as described here: https://toolset.com/forums/topic/parametric-search-with-filter-by-parent-attributes/

This is an attempt to achieve the same result as intended there by bringing the key data from the parent CPT into the Child CPT so that the desired filters can be set up.

What I am trying to set up is when someone completes a CRED form to create a new announcement, it picks up the value of the field "state" from the Parent CPT "company-profile" and saves it against the child CPT in the field "company-state"

The form already confirms that the announcement has a parent, gets the title of the parent CPT and then saves the parent title (wpcf_belongs_company-profile_id) from the Parent CPT against the Child CPT.

for your assistance here is the relevant code:

[cred-generic-field field="_wpcf_belongs_company-profile_id" type="hidden" class=""]
{
"required":1,
"validate_format":0,
"persist":1,
"default":"[cred-post-parent get='id']"
}
[/cred-generic-field]

[cred_show_group if="($(_wpcf_belongs_company-profile_id) eq '' )"]
NOTICE: To make an announcement you must be logged in, authorised and make the announcement via the "Make an Announcement" button on your company profile.
[/cred_show_group]

[cred_show_group if="($(_wpcf_belongs_company-profile_id) ne '' )"]

Company Name: [cred-post-parent get='title']

<div class="cred-field cred-field-_wpcf_belongs_company-profile_id">

</div>

Some code was added to the functions.php file but that was just to validate the child post had a parent.

I can't figure out how to get then save the contents of the "state" field in Parent CPT to "Company State field in Child CPT.

Please help.

#427065

Nigel
Supporter

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

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

Hi Peter

You want to duplicate the field from the parent post on the child post, if I have understood you correctly.

Before we go into how you might do that, can I check why?

Depending on what your final objective is, if you are displaying a child post (an announcement) somewhere, you should be able to retrieve the custom field of the parent post, rather than have the announcement carry a duplicate of the same custom field.

Is there a reason why you want to do that? What is the end objective?

#427071

Nigel

Currently the view to explore companies returns a random selection which can be filtered by state or category.

The announcements are returned in date order and I want to be able to apply a state filter so that the most recent announcements by date from the selected state are returned.

I went through this with Minesh here: https://toolset.com/forums/topic/parametric-search-with-filter-by-parent-attributes/ and wasn't able to come up with a satisfactory outcome using nested views so it seemed the only reasonable way to handle it seemed to be what I am trying to do now.

So when a new child post is created it grabs the fields from its parent that I want to be able to filter by.

I have a related challenge that my suggested solution may aid with ... which is being able to split the Announcements CPT RSS field into multiple feeds based on state.. which I guess will be much easier if the state field is at Announcement level rather than Parent CPT level.

Cheers

Peter

#427383

Nigel
Supporter

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

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

Hi Peter

OK, that sounds like reasonable cause.

For this you will need to add a custom shortcode to your functions.php file that will return the value of the required field from the parent which you can use in the CRED form.

So, I created a simple test site that has CPTs of company-profile and announcement.

I made one custom field group that will be used by both of these CPTs for the state (the slug for my state field is 'profile-state'). The state field type is 'checkboxes' and I set up the options, with "Western Australia" stored as "WA", "Queensland" stored as "QU" etc.

I created some company profiles and set the custom fields for the state.

Now, for the custom shortcode, I registered it with this code:

/**
 *  Custom shortcode to return field of parent post
 */
function show_parent_field_fn( $atts ) {

	// the parent is available as a url parameter
	$parent_id = $_GET['parent_company-profile_id'];

	// armed with parent id, get the state field of parent
	$parent_state = get_post_meta( $parent_id, 'wpcf-profile-state', true );
	$parent_state_values = array_values($parent_state);

	return $parent_state_values[0][0];
}
add_shortcode( 'show_parent_field', 'show_parent_field_fn' );

I auto-generated my form, which automatically added the field for the state. I'm going to pre-populate the value using my shortcode, like so:

[cred_field field='profile-state' post='announcement' value='[show_parent_field]' urlparam='']

Now, there is a slight snag.

The current version of CRED has a bug where the value parameter is not working correctly. It has been fixed in the next release, which is imminent.

As soon as CRED 1.8 is published this should work, but please let me know if you have any problems with it.

#427387

Nigel

Thanks.. that sounds great... subject to the snag.. any idea when CRED 1.8 is due out??

Cheers

Peter

#427390

Nigel
Supporter

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

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

Well, it was due this week, but we only release Mon-Weds, and I think some last minute snags in QA testing mean that it will be held over until Monday. That's the current expectation.

#427545

Very important issue for me

#427814

That would be great

I will test this then and see how I go...

Cheers

Peter

#427863

Nigel
Supporter

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

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

You might notice that the 2.2 release of Types snuck out overnight, but the rest of the Toolset suite (including CRED) is not expected until Monday.

#433612

Nigel

I have been away a week and have had some delays with other tech issues.

I have updated the plugins and just tried implementing your fix but I got the following error:

Parse error: syntax error, unexpected '&', expecting ']' on the following line:

$parent_id = $_GET['parent_company-profile_id'];

Could you suggest what changes I need to make to get this working?

Kind regards

Peter

#434012

Nigel
Supporter

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

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

That error message doesn't make much sense, there is no '&' in the line where it found an unexpected one.

Could you double-check you added the code correctly?

If you are sure you have then let me know and I will take a look.

I will mark your next reply as private so that I can get log-in credentials from you—you may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site, even though I don't intend to change things. Be sure to include FTP credentials.

Can you also confirm the url of where I can see the problem. What is the CRED form and where is it added?

#435526

Nigel

I did not copy the code correctly but I have identified a problem with what you have set up for my needs... Namely I already have the State field in use for the Company Profile CPT And I need to be able to keep using that existing field... to filter all company profiles by state (we have over 700).

I tried creating a new field group as you did - "profile-state", then deleting the existing "state" field from the "Company Profile" CPT and then renaming the new field group as "state". But I was unable to save the name change...

So what I have done is to edit the "Company Profile" Post Group so that it is used with both Company profile and Announcements... seem to be a dumb idea as I just want to make the "State" field work across both "company profile" and "announcements" CPTs. it is working... but the backend display of announcements is now very messy and you can edit the parent post from the Announcement ... not desirable... just want the State field not all.

Any idea how I can achieve my result with the existing "state" field from the parent CPT without making all parent fields work with the Announcements?

And another thing... I don't want the user to see or have the ability to change the state selection. How do I achieve this? thanks.

Cheers

Peter

#436282

Nigel
Supporter

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

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

Apologies for the delay in getting back to you, I have been absent for a couple of days and travelling all day today to our annual meet up.

I just wanted to take a moment to say I haven't forgotten you and will reply again as soon as I can.

Thanks for your patience.

#436529

Nigel
Supporter

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

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

Peter

In my test site I created a single custom field "profile-state" which was shared by both custom post types ("company-profile" and "announcement"), but there is no reason for you to change how you originally had it set up, with "state" as a custom field for "company-profile" and "company-state" as a custom field for "announcement".

You just need to modify the code snippet I provided and change the slug, so that you switch out 'wpcf-profile-state' for 'wpcf-state'.

And then when you use the shortcode on your child CRED form, you should have a line like so:

[cred_field field='company-state' post='announcement' value='[show_parent_field]' urlparam='']

Let me know if you have any problems once you made those switches.

#437512

All sorted.. finally

thanks so much for your help Nigel.

Just to address a part of my last message... how to hide cred fields that are being automatically populated... I found an answer here:

https://toolset.com/forums/topic/hidden-fields-in-cred/

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