Skip Navigation

[Resolved] Setting custom field based on another field value

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: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 2 years ago.

Assisted by: Luo Yang.

Author
Posts
#2529251

I have a custom post type called "Cars" that I am importing from a feed, so this insert is done only on the backend, no CRED form or anything.

I have created a custom field called "Company". I want to set the featured image (or some custom image field) to a specific URL if the Company field value is equal to "Company A" and to a different URL if the Company field value is equal to "Company B".

Any thoughts how I could set this up for inserts?

#2530111

Hello,

There isn't such kind of built-in feature, it depends on the plugin you "importing from feed".

If that plugin can trigger WordPress built-in action hook "save_post", you can use it to setup your custom codes and set custom fields as what you want.

More help:
https://developer.wordpress.org/reference/hooks/save_post/

#2533071

Yea I was seeing that. I am still new at WP action hooks in PHP. I was hoping to translate what I know in other systems/languages into WP. For example, some sort of business rule that I can implement that "On insert of a specific custom post type, add data to this field".

For example, is something like the code below going to trigger on insert of a new custom post type called "automobiles"?

function my_save_meta_function( $post_id, $post, $update )
{
if ( get_post_type( $post_id ) !== 'automobiles' ) return;
update_post_meta( $post_id, 'wpcf-model', 'Mustang' );
}
add_action( 'save_post', 'my_save_meta_function', 99, 3 );

#2533369

As I mentioned above, it depends on the plugin "importing from feed" you are using.

I am not sure if it can trigger WordPress built-in action hook "save_post", you will need to check the plugin author of "importing from feed".

In Toolset side, if the custom field is created with Toolset Types plugin, you just need to add "wpcf-" prefix before the field slug. See our document:
https://toolset.com/documentation/customizing-sites-using-php/functions/
when you are accessing custom fields through native WordPress functions, you need to prepend the wpcf- prefix to the slug. Continuing from the above example, for native WordPress function to access the “House Price” field, you need to use the wpcf-house-price slug.