Home › Forums › General Issues › Get_fields() returns false until I update the post › Reply To: Get_fields() returns false until I update the post
I just tested acf_maybe_get_field() on a field that exists but was never saved. The function returns false. I have no idea how you are getting the field key by using that function if the field has no value.
Under these conditions this line should be causing a PHP Undefined Index Warning:
return $maybe_field_obj['key'];
There is no reliable way that I know of using just the field name to get a field key for a field that does not have a value for a specific post.
You could potentially use acf_maybe_get_field() if you supplied a post ID for a post that you know will have the value set. From what I’m hearing there would be no way to know what post ID to use.
You could possibly do a query to get a post where the field key references already exist. Let’s assume that you know that if the field “email” exists for a post and has a field key reference in the db that all other fields will also exist.
$args = array(
'post_type' => 'your-post_type',
'posts_per_page' => 1,
'no_found_rows' => true,
'meta_query' => array(
array(
'key' => '_email'
'compare' => 'EXISTS'
}
)
);
$test_query = new WP_Query($args);
if (!empty($test_query->posts) {
$valid_fields_post_id = $test_query->posts[0]->ID;
}
But this will not work unless you can be sure all of the field references exist. You could also check for the existence of all field key references but I think if you have too many fields you need to check that the query could time out.
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.