How is clicking 'update' on a post different from programatically creating posts?
Asked Answered
E

2

6

hoping for some advice.

I'm programmatically inserting a large number of posts into wordpress from a JSON feed. The wp_insert_post function is working brilliantly and the posts are created, along with correctly populated Advanced Custom Fields meta data.

We have a strange issue by which until we manually click "update" on a single post the custom fields aren't available using a JSON API plugin.

I've tried updating all via the bulk editor, as well as calling wp_update_post after the JSON import. It's as if the act of clicking "update" on a single post saves the post in a different fashion.

Can anyone advise why this would be the case? Any advice or pointing in the right direction would be greatly appreciated!


EDIT: the code we're using to update our post meta...

function __update_post_meta( $post_id, $field_name, $value = '' ) {
        if ( empty( $value ) OR ! $value )
        {
                delete_post_meta( $post_id, $field_name );
        }
        elseif ( ! get_post_meta( $post_id, $field_name ) )
        {
                add_post_meta( $post_id, $field_name, $value );
        }
        else
        {
                update_post_meta( $post_id, $field_name, $value );
        }
}
Encratia answered 1/4, 2015 at 22:3 Comment(7)
wp_update_post doesn't handle meta data (where ACF fields are stored), you're looking for update_post_meta. There isn't a bulk update_post_meta, unfortunately you'll have to loop through each key separately.Byrnes
Thank you for your response! I'm using update_post_meta in the function that loops over the custom fields we're bringing in, and after a successful import, the data is all present and correct. It just doesn't seem to be available via the API until we literally edit and update the post manually. If the data is in there on first import, has the update_post_meta function not completed successfully? After the import has run and been successful, would we need to somehow run the wp_update_post again?Encratia
update_post_meta is run when you click "update", which is why I believed that might be your issue. Do you have revisions/autosaves enabled? Which plugin are you using?Byrnes
The plugin we're using for the API is wp-api.org, with this snippet to add the ACF fields to it. I'll edit the question to show the code we're using to update the post meta...Encratia
@JesseKernaghan - you mentioned revisions and autosaves - do you know if this interferes with programmatically inserted posts? Just wondered why you mentioned it!Encratia
When you use wp_update_post it automatically duplicates and revisions if you have that enabled. I had thought perhaps the metadata wasn't getting updated with it, but that shouldn't be possible.Byrnes
I would really like to know the real answer to that question!Bennie
E
1

So we resolved the particular issue we were having.

When a post is programmatically created with ACF fields, the posts are not labelled in the same way as standard post meta. Read here for more info on that!

Updating a post manually creates the necessary "aliases". Until that point, if you want to get the info out, you need to reference ACF's initial "fieldXXXXXXXX" post meta key.

Encratia answered 6/4, 2015 at 17:1 Comment(4)
Can you share the actual solution you used? it might help us also... thanks.Bennie
@Bennie we didn't end up programming a solution, we just bulk updated the posts. They may be a way to programmatically 'resave' which assigns the necessary ACF keys.Encratia
Jesus i must update thousands of posts. Somebody post a solution!Shakitashako
@MichaelRogers same here. Switched from acf-json to wordplate/Acf-extended. The data is still there but doesnt get displayed until i hit the "update" button.. Help!Prang
C
0

Here is a way to automate the process of re-assigning ACF field references. The idea is to list all the fields in a group and use ACF's internal keys system to update the corresponding post field values. Here is a simple example for an entire ACF field group:

// Find the concerned group key at: /wp-admin/edit.php?post_type=acf-field-group
// You could probably also automate this if needed by calling acf_get_field_groups()
$groupKey = 'group_6662fe807b8b8'; 

// Get all posts you want to update
$posts = get_posts(array(
    'post_type' => 'post',
    'numberposts' => -1,
));

// Update post fields based on the field key
foreach ( $posts as $post ) {
    $groupFields = acf_get_fields($groupKey);
    foreach($groupFields as $field) {
        $name = $field['label'];
        $key = $field['key'];
        $value = get_field($key, $post->ID);
        update_field($name, $value, $post->ID);
    }
}
Chiller answered 31/8, 2024 at 13:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.