Home › Forums › General Issues › How do I use acf/load_value correctly?
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.
Hi @Ian
It looks like you are writing a primitive version of the get_field function ( $wpdb->get_var()
).
Perhaps you could use the get_field function instead? You can use the $post_id parameter to target the THE-BUSINESS-PROFILE-CPT-ID
Hope that helps.
Thanks
E
Hi Elliot thanks for the heads-up and first-class support.
For those that are interested, I resolved my issue with the following code:
function MY_FUNCTION_NAME( $value ) {
if ( !$value ) : // IF VALUE EMPTY, RETURN NEW VALUE
$value = get_field( 'MY_CUSTOM_FIELD_NAME', $POST_ID) );
return $value;
endif;
return $value; // RETURN EXISTING VALUE
};
add_filter( 'acf/load_value/name=MY_CUSTOM_FIELD_NAME', 'MY_FUNCTION_NAME' );
The topic ‘How do I use acf/load_value correctly?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.