简体   繁体   中英

ACF relationship field with add_post_meta

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM