【问题标题】:How to set custom fields value while inserting new post? Wordpress PHP插入新帖子时如何设置自定义字段值? WordPress PHP
【发布时间】:2020-05-23 17:54:42
【问题描述】:

我的代码有点问题。我想插入带有自定义字段值的新帖子,但这些自定义字段不是来自 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
));

【问题讨论】:

标签: php wordpress themes custom-fields


【解决方案1】:

解决此问题的第一件事是手动输入以确保您正确使用 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.
));

【讨论】:

  • 是的,没有这些值它可以正常工作。我得到了正确的 wordpress 插入内容,例如:描述和标题。主题中的自定义字段不会像这样工作。
  • OK,那么自定义主题没有重载 wp_insert_post()。所以,你不能那样做。您唯一的字段选项是此处文档中的选项:developer.wordpress.org/reference/functions/wp_insert_post您必须将有关电影的所有信息放入 post_content
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多