我的代码有点问题。我想插入带有自定义字段值的新帖子,但是这些自定义字段不是来自Wordpress,而是来自主题。像update_meta_value()这样的简单解决方案不起作用。我什至找不到主题脚本来设置这些字段的值。我只找到了这些代码中的大部分,在我想要的字段中关注什么。
array(
'id' => 'movie_default_fields',
'type' => 'select',
'class' => 'chosen',
'title' => esc_html__('Enable Default Fields', 'amy-movie'),
'options' => array(
'movie_release' => esc_html__('Release Date', 'amy-movie'),
'movie_duration' => esc_html__('Duration', 'amy-movie'),
'movie_imdb' => esc_html__('IMDB', 'amy-movie'),
'movie_mpaa' => esc_html__('MPAA', 'amy-movie'),
'movie_language' => esc_html__('Language', 'amy-movie'),
'movie_genre' => esc_html__('Genre', 'amy-movie'),
'movie_actor' => esc_html__('Actor', 'amy-movie'),
'movie_director' => esc_html__('Director', 'amy-movie'),
),
'attributes' => array(
'multiple' => 'multiple',
),
'default' => array('movie_release', 'movie_duration', 'movie_imdb', 'movie_mpaa', 'movie_language', 'movie_genre', 'movie_actor', 'movie_director')
),
if (!empty($custom_fields)) {
foreach ($custom_fields as $field) {
if ($field['type'] == 'text') {
$name = (isset($field['name']) && $field['name'] != '') ? $field['name'] : '';
$movie_field[] = array(
'id' => sanitize_title($name),
'type' => 'text',
'title' => $name,
);
}
}
}
它以特定的自定义帖子类型显示自定义字段的列表。
if (!function_exists('amy_movie_defaults_fields')) {
function amy_movie_defaults_fields() {
$default = array(
'movie_release',
'movie_duration',
'movie_imdb',
'movie_mpaa',
'movie_language',
'movie_genre',
'movie_actor',
'movie_director'
);
return $default;
}
}
if (in_array('movie_duration', $defaults_fields)) {
$movie_field[] = array(
'id' => 'movie_duration',
'type' => 'text',
'title' => esc_html__('Duration', 'amy-movie'),
'after' => '<em>min</em>',
);
}
我试图在自动创建帖子时尝试插入这些值,但不起作用
$post_id = wp_insert_post(array (
'post_type' => 'amy_movie',
'post_title' => $info->title,
'post_content' => $desc->description,
'post_status' => 'publish',
'movie_duration' => $info->duration, //simple example
));
我要解决此问题的第一件事是放入手动条目,以确保您正确使用了wp_insert_post()函数。自己尝试一下,看看是否可行。如果确实如此,那么您只是没有从正确的位置或正确的方式提取所需的值:
$post_id = wp_insert_post(array (
'post_type' => 'amy_movie',
'post_title' => 'The Big LeBowski',
'post_content' => 'Movie Duration: 1h 5m',
'post_status' => 'publish',
//'movie_duration' => $info->duration, //try it without this first. This function would have to be overloaded by the theme for this to work.
));