简体   繁体   English

具有add_post_meta的ACF关系字段

[英]ACF relationship field with add_post_meta

i try to create a wordpress post by prog. 我尝试通过编创建wordpress帖子。

First, i use WP_insert_post to create the post, and it return me the id of the post just created. 首先,我使用WP_insert_post创建帖子,它返回我刚创建的帖子的ID。

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: 之后,我想将信息添加到帖子的自定义字段中,因此我使用ID和add_post_meta像这样:

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

For a WYSIWUG Editor or textarea or a textfield ACF, it working fine. 对于WYSIWUG编辑器或textarea或textfield ACF,它可以正常工作。

But i also have a relationship ACF and, i try with add_post_meta like for the textarea, but it not working! 但是我也有一个ACF关系,并且我尝试使用add_post_meta类的textarea,但是它不起作用!

How can i make add_post_meta work with relationship ACF? 如何使add_post_meta与关系ACF一起使用?

I prefer to use update_post_meta for inserting ACF values. 我更喜欢使用update_post_meta插入ACF值。 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. 您的问题可能是WP在具有该ACF文本区域的帖子上创建具有默认元值的元键,并且当您调用add_post_meta时,您并未为该元键分配元值,而是创建了一个全新的键/值对。

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

Docs for update_post_meta can be found here . 可在此处找到update_post_meta的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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