【发布时间】:2015-12-01 19:21:42
【问题描述】:
我正在使用 PHP 自动化来创建新的 Wordpress 帖子(使用 wp_insert_post)。我还使用“简单字段”添加额外的元字段以在前端显示。
我的代码创建了 Wordpress 帖子,将帖子 ID 保存在一个变量中,然后立即继续添加所需的元数据。
整理所有其他混乱,这是我的过程:
// Insert the post into the database and return the post ID
$post_id = wp_insert_post( $my_post );
// Add '$price' to relevant meta field
update_post_meta($post_id, '_simple_fields_fieldGroupID_1_fieldID_4_numInSet_0', $price);
这很好用,当我检查数据库时,“价格”元字段插入了正确的信息。
当我尝试使用以下方法提取它时,问题就开始了:
echo simple_fields_value("price", $post->ID);
代码有效 - 但我可以让它提取数据的唯一方法是登录到 Wordpress 管理员,打开帖子进行编辑,然后点击“更新”。
如果我打开每个帖子并点击“更新”,我的代码就没有问题...它按预期执行所有操作。
我的问题是,我没有使用“更新”按钮做什么?我尝试使用“wp_update_post”重新打开帖子并在添加元数据后保存以查看是否有帮助,但没有。
// Open and update post to ensure meta data saves correctly
$update_post = array(
'ID' => $post_id,
'post_title' => $title
);
// Update the post into the database
wp_update_post($update_post);
非常感谢您的帮助,
【问题讨论】:
-
不是 100% 确定,因为我从未使用过简单的字段 - 但是如果您尝试使用 developer.wordpress.org/reference/functions/get_post_metaget_post_meta() 来获取您的数据会怎样。我假设 simple_fields_value() 是这里的问题,因为您的其余代码看起来不错。
-
抱歉,Simon - 我以为您在下面创建了我刚刚标记为正确的答案。你首先做对了,不幸的是你没有把它作为答案。非常感谢!
-
别担心,伙计——只要你最终到达那里 :) 他提到的 '_simple_fields_been_saved' 元值无论如何都值得注意。