1
votes

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).

1
Show us your code. You say you tried get_fields(), but how did you use it after that?disinfor
Edit your question to show your code. Without it, we are only guessing on what you've done.disinfor
Your code still has not been added. Just putting in the functions you've tried doesn't help. How did you use those functions? Show us some context. We can't help until we have more information.disinfor
I've run into similar issues where the functions are not hooked late enough when called from another plugin. You need to either use a later hook such as acf/init, or you need to use the default get_post_meta() function instead (bear in mind you need to prepend the group key to the subfield key for groups: bohanintl.com/wptips/plugins/…)Shoelaced
That's not how you use get_fields. You don't pass it a field name - it returns all fields by default. Do a var_dump(get_fields()); and let us know what it shows.FluffyKitten

1 Answers

1
votes

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