Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress ACF get_field( ) not returning value

I am using the advanced custom field plugin for Wordpress. I am having difficulty displaying a field on my page.

Basically I've created a field group and assigned id's to the members of that group. I then use the get_field('field_name') function to store the value of this field in a variable and echo it on the screen. However this is returning false.

I've also tried using the_field('field_name') but this returns null. I then read somewhere If you are trying to access a field outside of the Wordpress loop you must pass the post id as a parameter to the get_field()/the_field() methods.

I've tried that and still the same result...Does anyone have any idea as to what is the problem?

This is my code:

<?php get_header();
      $postID = get_the_ID();
      the_field('the-title', $postID); //Nothing being returned...
      die(); 
?>
like image 597
Javacadabra Avatar asked Jul 22 '14 12:07

Javacadabra


People also ask

Where does ACF store field data?

ACF stores the data for post custom fields in the post_meta table. It stores the data for all other custom fields in the options table. All the ACF data can be accessed using standard WordPress functions like get_post_meta and get_option if you use the correct meta_key or option_name.

How do I get ACF field value in WordPress?

Because ACF will format the value depending on the field type and make development quicker and easier! To retrieve a field value as a variable, use the get_field() function. This is the most versatile function which will always return a value for any type of field.


2 Answers

If you're using WP_Query() before using get_field(), you need to reset the query using wp_reset_query() function. I hope it'll solve this issue.

like image 114
Jitu Raiyan Avatar answered Oct 31 '22 20:10

Jitu Raiyan


You're using get_the_ID() outside of the loop.

http://codex.wordpress.org/Function_Reference/get_the_ID

You could try:

global $post;
the_field( 'the-title', $post->ID );

But this would depend on what page you're on.

Which template file is this being used in?

like image 22
Nathan Dawson Avatar answered Oct 31 '22 22:10

Nathan Dawson