Skip Navigation

[Resolved] Auto-add a field to a CPT

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.

This topic contains 16 replies, has 3 voices.

Last updated by camila 11 months, 2 weeks ago.

Assigned support staff: Minesh.

Author
Posts
#2191091

Hello!
I have 2 CPT with a One to many relationship.
Is it possible to auto populate a field from one of the CPT to another one?
Please let me know,
thanks

#2191673

Jamal
Supporter

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

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

Hello and thank you for contacting Toolset support.

Well, there is no built-in feature to copy a field from a post to a related post. But, you can achieve it with custom code.

Depending on how you want to implement this, you will have to run your code into a different hook:
- If you are using a Toolset Form, you can rely on the cred_save_data https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
- If you are saving post from the backend, you can rely on the save_post hook https://developer.wordpress.org/reference/hooks/save_post/
- Or when two posts are related, you can rely on the toolset_association_created https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created

Inside your code, you can get the related post using the Toolset relationship API, especially the toolset_get_related_post function
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

If you are unsure what to do, please provide more details about your use case, and I'll do my best to help.
What post types? What is the relationship slug? What are the custom field slugs in both types?

#2192973

Hi Jamal,
thanks for the answer I can give you access to the site, but basically I have 2 CPT:
1. Tax Sales
2. Properties
- We are saving posts from the backend
- 1 Tax Sale can be assigned to one Property, Infinite Properties can be assigned to one Tax Sale
- Relationship slug: tax-sale-property
- Custom field in the Tax Sale Record: tax-sale-date
- Custom field in the Property Record: property-sale-date
- We want to copy the tax-sale-date to the property-sale-date where there is a relationship.
Thanks

#2193067

Jamal
Supporter

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

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

I assume that the tax-sale-date will already be set on the Tax Sale post, right!
This means that when an association (Tax Sale<>Property) is set, the value of "tax-sale-date" will already exists for the parent post.
If yes, the following code should be enough:

add_action( 'toolset_association_created', 'update_field_after_association_created', 10, 5 );
function update_field_after_association_created( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
    if ( $association_slug == 'tax-sale-property'){
		// get the value from the parent post
		$date = get_post_meta( $parent_id, 'wpcf-tax-sale-date' );
		//update the child post
		update_post_meta( $child_id, 'wpcf-property-sale-date', $date );
	}
}

Notice how custom fields slugs are prefixed with 'wpcf-' at the database level.

If not, then additional code should be produced. And should be run when the Tax Sale or the Property post is saved. https://developer.wordpress.org/reference/hooks/save_post/

Check these articles too:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/

Please note that custom code is out of the scope of support. If you need advanced customizations, consider hiring a developer. https://toolset.com/toolset-support-policy/
Here is a list of our partners https://toolset.com/contractors/

#2195747

Hi Jamal, did you have some time to look at the issue?
thanks

#2197003

Jamal
Supporter

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

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

Hello and my apologies for the late reply, but I do not work on Wednesdays and Thursdays as you may check on my profile page https://toolset.com/forums/users/jamal-b/

I could not find where did you add the code. It is not on Toolset->Settings->Custom code section, and I don't have access to the theme's editor or the plugins' editor to check if it was added there.

I tried to add the snippet in Toolset->Settings but I am facing an error. Most probably because you are disabling the editor on the network. Check this screenshot hidden link

To better assist you further, I'll also need FTP access to this network. I am setting your next reply to be private. Please add FTP credentials to it.

#2197741

Jamal
Supporter

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

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

The only way I can debug the code on your website is using the logs. However, I can't activate it on the wp-config.php file. The hosting is somehow configured to disallow code update at all.

Can I take a copy of your website and debug it locally? Or migrate your website to our online platform and work on it there.

#2197817

Migrating to your platform is no problem.

Do you want me to send you an export, or can you grab it?

#2197827

... or work on it locally: whatever is going to be easier/most efficient.

I can also make a copy on another server will less restrictions if its easier.

#2198159

Jamal
Supporter

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

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

Thank you for your feedback. I could not make a copy or migrate your website as it needs a plugin for it. For migrating we use the CouldWays plugin. And for making copies, we use Duplicator or All In One Migration.

Can you install these plugins or prepare a server with less restrictions? Personally, I would prefer to work in my local development environment. Even if I migrate your website to our platform, I'll take a copy from it and work on it locally.

#2198165

I've just installed all 3. I'm not sure how well they work with multisite, but please give it a try.

#2198191

Jamal
Supporter

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

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

The free version of Duplicator does not handle Multisite. I believe All In One Migration can copy a subsite of a multisite install.

I just logged in but, I don't see any of the plugins. Can you double-check from your side?

#2198247
Screen Shot 2021-10-17 at 9.33.21 AM.png
Screen Shot 2021-10-17 at 9.31.32 AM.png

All in one needs a paid addon as well.

Cloudways is there, you just need to go to the network dashboard to access it

#2198361

Jamal
Supporter

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

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

Unfortunately, I don't have access to the Network menu. So, I created a new installation with similar post types and custom fields, and I find out the issue with the code.
The issue is with line 5, which returns an array of values, instead of a single value. I added the 3rd argument to make it add one value.

add_action( 'toolset_association_created', 'update_field_after_association_created', 10, 5 );
function update_field_after_association_created( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
    if ( $association_slug == 'tax-sale-property'){
        // get the value from the parent post
        $date = get_post_meta( $parent_id, 'wpcf-tax-sale-date', true );
        //update the child post
        update_post_meta( $child_id, 'wpcf-property-sale-date', $date );
    }
}

Please give it a try and let me know if it works as expected.

Please note that if you connected the Tax Sale from the Property post, then you will have to reload the post to see the saved value.

#2200757

Hi Jamal, thanks so much for your help, it's working. we just realized while testing that we need to use copy the field when saving the post, I am guessing is the same logic right?