Hi Guys, I’m new to ACF and was looking for a little help…
I am working on as site that allows users to register their Business Profile as a custom post type (CPT) (not User profile) – FYI: I can’t add the Business details to the User Profile as there are over 50 custom fields and I need to control layout for users.
Once registered, the Business User can create several CPT’s which all require some default custom fields that have been specified in their Business Profile CTP.
I would like to know how to automatically fill in a custom field on all future CPT’s with the value of the related field on their Business Profile CPT, when a new CPT is created.
Currently I’m hooking into: acf/load_value/name=MY-CUSTOM-FIELD, but I’m using the following code to achieve this and I’m sure there’s a better way:`function MY_FUNCTION_NAME() {
global $wpdb;
return $value = $wpdb->get_var( “SELECT meta_value FROM $wpdb->postmeta WHERE post_id='” . THE-BUSINESS-PROFILE-CPT-ID . “' AND meta_key='REQUIRED-META-KEY'” );
};
// CHECK IF FIELD VALUE IS BLANK USING $_GET['post']
$check = $wpdb->get_var( “SELECT meta_value FROM $wpdb->postmeta WHERE post_id='” . $_GET['post'] . “' AND meta_key='REQUIRED-META-KEY'” );
// IF CHECK IS NULL APPLYING FILTER – FOR NEW AND EXISTING POSTS
if ( ( !$check ) && ( get_post_type( $_GET['post'] ) == 'MY-CPT' ) || $_GET['post_type'] == 'MY-CPT' ) :
add_filter( 'acf/load_value/name=MY-CUSTOM-FIELD', 'MY_FUNCTION_NAME' );
endif;`
I hope you can help.
Ian.