Home › Forums › Front-end Issues › Retrieving saved ACF values › Reply To: Retrieving saved ACF values
The problem is that you are not in “The Loop”. This is a WP concept that you may not be aware of since you are new to WP https://codex.wordpress.org/The_Loop and this is why you can get the value if you supply the ID. If you are outside of “The Loop” then ACF does not know what post to get the value from. You must have a loop, even for showing a single post, an all things related to this post must be done in “The Loop”, or you must supply the ID so that WP and ACF knows what post you are working with.
// outside the loop
// the post ID of the current post is not known
$value = get_field('my-field'); // will return nothing here
// The Loop
if (have_posts()) {
while (have_posts()) {
the_post();
// this is required even in single post templates
$value = get_field('my-field'); // returns field value for this post
}
}
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.