Reputation: 1460
I am working on a plugin to add custom post data to the front end. I am using the Advanced Custom Fields plugin to add custom fields to the editor.
After updating the post, I should get all custom field values via get_post_meta()
but it is only showing the default meta fields, not the custom fields. I have two individual group fields, each with 2 text fields. I was expecting to get an array or object.
I tried to add a single text field and add data to it, just to see if the group fields are causing any problem. But no luck.
I have tried get_field()
, the_field()
and get_sub_field()
functions from the ACF website but none are working.
Edit:
This is the code using get_post_meta()
<?php
global $post;
$temp = get_post_meta($post->ID);
/* PRINT THE ARRAY */
echo "<pre>";
print_r($temp);
echo "</pre>";
?>
This is the code using get_field()
<?php
$temp = get_field("field1"); // 'field1' is a one simple text field.
/* PRINT THE ARRAY */
echo "<pre>";
print_r($temp);
echo "</pre>";
?>
This is the code using the_field()
<?php
$temp = the_field("field1"); // 'field1' is a one simple text field.
/* PRINT THE ARRAY */
echo "<pre>";
print_r($temp);
echo "</pre>";
?>
This is the code using get_sub_field()
<?php
/* 'section_1' is a group consist of 2 text fields + another group with 2
text field. */
$temp = the_field("section_1");
/* PRINT THE ARRAY */
echo "<pre>";
print_r($temp);
echo "</pre>";
?>
Note: The above code is in a file located in a sub folder of the main plugin. The thing I am trying to do is to change the layout of default blog page. In plugin's function.php I have changed the default layout path with my custom file path.
This is function.php
add_filter('single_template', 'my_custom_template', 99);
function my_custom_template($single) {
global $post;
if ( $post->post_type == 'post' ) {
if ( file_exists( plugin_dir_path(__FILE__) . '/templates/style1/style1.php' ) ) {
return plugin_dir_path(__FILE__) . '/templates/style1/style1.php';
}
}
return $single;
}
Update When I try var_dump(get_fields());
it returned bool(false)
.
Upvotes: 1
Views: 3991
Reputation: 14312
ACF fields are not saved as post meta, they use their own custom fields in the WP database. Therefore you need to use the ACF functions to get the values.
As we discussed in the comments, if you are not using the ACF functions in the Wordpress "loop", you need to pass in the post id, e.g.
// post id is the 2nd parameter, but it is only needed if you are not in the loop
$fieldvalue = get_field($fieldname, $postid);
echo $fieldvalue;
or
// also note that the_field doesn't return the value like get_field -
// it prints it out just like if you used echo with get_field
the_field($fieldname, $postid);
For get_fields
, it doesn't take a field parameter at all because it returns all fields in an array. You can pass the post id as the first parameter if you need it, e.g.
$allfields = get_fields($posttestid);
print_r($allfields);
You can see more about the ACF functions here
Upvotes: 1