php - 高级自定义字段和前端发布
问题描述
我创建了一个创建新的自定义帖子类型单一的前端表单。无论如何要从这种形式更新 ACF 元?
<?php if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
// Add the content of the form to $post as an array
$post = array(
'post_title' => $description,
'post_content' => $description,
'post_category' => 3, // Usable for custom taxonomies too
'post_status' => 'private', // Choose: publish, preview, future, etc.
'post_type' => 'door', // Use a custom post type if you want to
'post_parent' => $post->ID,
'post_name' => 'service',
'meta_key' => 'archive_job',
'meta_value' => 'red',
);
wp_set_object_terms($pid, $_POST['terms'], 'child');
wp_insert_post($post); // Pass the value of $post to WordPress the insert function
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
wp_redirect( $current_url );
// http://codex.wordpress.org/Function_Reference/wp_insert_pos
} // end IF
// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post', 'add_post_meta' , 'update_post_meta');
?>
解决方案
ACF 元作为基本自定义字段保存在数据库中。因此,您可以使用例如函数 add_post_meta() 与值 add_post_meta($post_id, 'field_name', $field_value); 在 wp_insert_post($post) 之后;功能。
示例代码:
$post = array(
'post_title' => $description,
'post_content' => $description,
'post_category' => 3, // Usable for custom taxonomies too
'post_status' => 'private', // Choose: publish, preview, future, etc.
'post_type' => 'door', // Use a custom post type if you want to
'post_parent' => $post->ID,
'post_name' => 'service',
'meta_key' => 'archive_job',
'meta_value' => 'red',
);
wp_set_object_terms($pid, $_POST['terms'], 'child');
$post_id = wp_insert_post($post); // Pass the value of $post to WordPress the insert function
add_post_meta($post_id, 'field_name', $field_value);
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
wp_redirect( $current_url );
推荐阅读
- django - 位置参数缺少“状态”
- java - 如何让 Spring Tools Suite 识别 spring 注释
- mongodb - Mongo DB中基于光标的分页,其中跳过或限制未知
- laravel - 如何在 plesk 服务器上处理 SQLSTATE[HY000] [1045] 错误?
- javascript - 使用 HTML、CSS 和 JS 的侧边栏中的多级下拉菜单
- elixir - 模块执行时如何将当前上下文带入模块中?
- linux - 如何检查文件是否来自 tmpfs 内存缓存或磁盘缓存
- r - ak fold cross validation 与开发集中的 AUC/Sensitivity/Specificity 值有何不同?
- google-cloud-platform - Deploying a Cloud Function from Cloud Build with --allow-unauthenticated
- python - 在两个不同的数据框python中获取匹配的行