get_field()
This function will return a custom field value for a specific field name/key + post_id. There is a 3rd parameter to turn on/off formating. This means that an image field will not use its 'return option' to format the value but return only what was saved in the database
No Hooks.
Returns
(Mixed).
Usage
get_field( $selector, $post_id, $format_value );
- $selector(required)
- .
- $post_id
- .
Default:false - $format_value
- .
Default:true
Changelog
| Since 3.6 | Introduced. |
get_field() get field code ACF 6.0.4
function get_field( $selector, $post_id = false, $format_value = true ) {
// filter post_id
$post_id = acf_get_valid_post_id( $post_id );
// get field
$field = acf_maybe_get_field( $selector, $post_id );
// create dummy field
if ( ! $field ) {
$field = acf_get_valid_field(
array(
'name' => $selector,
'key' => '',
'type' => '',
)
);
// prevent formatting
$format_value = false;
}
// get value for field
$value = acf_get_value( $post_id, $field );
// format value
if ( $format_value ) {
// get value for field
$value = acf_format_value( $value, $post_id, $field );
}
// return
return $value;
}