我正在尝试使用wp\\u insert\\u post创建前端提交表单,以便访问者可以创建帖子并更新该帖子的元框字段--not the custom fields.
例如,当我在提交表单中使用以下内容时。。。
<?php update_post_meta($post_id, $meta_key, $meta_value); ?>
。。。它更新了新创建的帖子的内置自定义字段部分,但没有更新我创建的元框。有什么想法吗?我正在尝试使用wp\\u insert\\u post创建前端提交表单,以便访问者可以创建帖子并更新该帖子的元框字段--not the custom fields.
例如,当我在提交表单中使用以下内容时。。。
<?php update_post_meta($post_id, $meta_key, $meta_value); ?>
。。。它更新了新创建的帖子的内置自定义字段部分,但没有更新我创建的元框。有什么想法吗?它都进入同一个表,您可能没有使用正确的键名来插入它。唯一区别自定义字段键和用于元盒的键的是,元盒键通常以下划线作为前缀,以从显示在“自定义字段”部分的列表中隐藏它们。还可以在单个条目下将多个内容存储为一个数组。如果您使用某种类型的metabox助手类来生成metabox,则可能会出现这种情况。直接在表中查看metabox数据的存储方式。
下面是我用来从前端表单添加帖子的一些代码,该表单还添加元数据和分类术语。请注意,代码段提取了所有安全性、数据验证和数据清理。它只显示了我对数据所做的操作,以便在清理完所有数据后将其添加到数据库中。
// Submit the values if there are no errors
if(empty($errors))
{
// Prepare title
$term = get_term_by(\'id\', $values[\'complaint_type\'], \'complaint-type\');
$title = $values[\'address_clean\'].\' (\'.$term->name.\')\';
// Gather post data
$post = array(
\'post_title\' => $title,
\'post_content\' => $values[\'description\'],
\'post_status\' => \'publish\',
\'post_type\' => PREFIX_POST_TYPE_NAME,
\'post_author\' => 1
);
// Attempt to add post
if($id = wp_insert_post($post))
{
// Add metadata to post
update_post_meta($id, \'_\'.PREFIX_PLUGIN_NAME_L.\'_latitude\', $values[\'latitude\']);
update_post_meta($id, \'_\'.PREFIX_PLUGIN_NAME_L.\'_longitude\', $values[\'longitude\']);
// Associate complaint-type
if(!wp_set_post_terms($id, $values[\'complaint_type\'], \'complaint-type\'))
$errors[\'wp_set_post_terms\'] = \'There was an error adding the complaint type.\';
}
else
$errors[\'wp_insert_post\'] = \'There was an error adding the complaint.\';
}