1

i try to create a wordpress post by prog.

First, i use WP_insert_post to create the post, and it return me the id of the post just created.

After that, i want to add information to the custom field on the post, so i use the ID and add_post_meta like that:

add_post_meta( $post_id, 'info', 'This is my value' );

For a WYSIWUG Editor or textarea or a textfield ACF, it working fine.

But i also have a relationship ACF and, i try with add_post_meta like for the textarea, but it not working!

How can i make add_post_meta work with relationship ACF?

Anoop Asok
  • 1,311
  • 1
  • 8
  • 20
Gustav
  • 79
  • 2
  • 12

1 Answers1

1

I prefer to use update_post_meta for inserting ACF values. The first thing this function does is check if the meta key already exists, and it either creates a new one or updates the meta value.

Your problem might be that WP is creating the meta keys with a default meta value on posts with that ACF textarea, and when you call add_post_meta you're not assigning a meta value to that meta key but creating a completely new key/value pair.

Example: <?php update_post_meta($post_id, $meta_key, $meta_value) ?>

Docs for update_post_meta can be found here.

Mike Kauffman
  • 276
  • 1
  • 5
  • 1
    Actually i found why, it because normal ACF field like textarea or textfield, take a simple meta_value as a string, but for the relational ACF, it take a PHP serialize array that contain the postID of the page you want to add in relation with your Posts! – Gustav Dec 01 '14 at 20:11