acf_maybe_get_field()
This function will return a field for the given selector. It will also review the field_reference to ensure the correct field is returned which makes it useful for the template API
No Hooks.
Returns
$field. (array)
Usage
acf_maybe_get_field( $selector, $post_id, $strict );
- $selector(required)
- .
- $post_id
- .
Default:false - $strict
- .
Default:true
Changelog
| Since 5.2.3 | Introduced. |
acf_maybe_get_field() acf maybe get field code ACF 6.8.8
function acf_maybe_get_field( $selector, $post_id = false, $strict = true ) {
// init
acf_init();
// Check if field key was given.
if ( acf_is_field_key( $selector ) ) {
return acf_get_field( $selector );
}
// Lookup field via reference.
$post_id = acf_get_valid_post_id( $post_id );
$field = acf_get_meta_field( $selector, $post_id );
if ( $field ) {
return $field;
}
// Lookup field loosely via name.
if ( ! $strict ) {
return acf_get_field( $selector );
}
// Return no result.
return false;
}