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)
.
get_fields()
, but how did you use it after that? – disinforacf/init
, or you need to use the defaultget_post_meta()
function instead (bear in mind you need to prepend the group key to the subfield key for groups: bohanintl.com/wptips/plugins/…) – Shoelacedget_fields
. You don't pass it a field name - it returns all fields by default. Do avar_dump(get_fields());
and let us know what it shows. – FluffyKitten