我在WordPress中创建了一个自定义的帖子类型,并附带了一个自定义的分类法。
使用wp\\u insert\\u post()从网站的“发件人”结尾处获取帖子收集日期。
这是我的自定义后期注册代码:
$labels = array(
\'name\' => \'Films\',
\'singular_name\' => \'Film\',
\'add_new\' => \'Add new\',
\'add_new_item\' => \'Add new film\',
\'edit_item\' => \'Edit film\',
\'new_item\' => \'New film\',
\'all_items\' => \'All films\',
\'view_item\' => \'View film\',
\'search_items\' => \'Search films\',
\'not_found\' => \'No films found\',
\'not_found_in_trash\' => \'No films found in Trash\',
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Films\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => null,
\'taxonomies\' => array(\'film_category\'),
\'supports\' => array(
\'title\',
\'editor\'
)
);
register_post_type(\'film\', $args);
这是我的自定义分类注册码:
$labels = array(
\'name\' => \'Category\',
\'singular_name\' => \'Film\',
\'search_items\' => \'Search Categories\',
\'all_items\' => \'All Categories\',
\'parent_item\' => \'Parent Category\',
\'parent_item_colon\' => \'Parent Category:\',
\'edit_item\' => \'Edit Category\',
\'update_item\' => \'Update Category\',
\'add_new_item\' => \'Add New Category\',
\'new_item_name\' => \'New Category Name\',
\'menu_name\' => \'Category\'
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'rewrite\' => false,
\'capabilities\' => array(\'manage_terms\')
);
register_taxonomy(\'film_category\', array(\'film\'), $args);
两者都是在初始化操作时激发的。
现在,当最终用户从前端提交表单时,我执行该代码:
$post = array(
\'comment_status\' => \'closed\',
\'ping_status\' => \'closed\',
\'post_author\' => 2,
\'post_content\' => $_POST[\'synopsis\'],
\'post_name\' => sanitize_title($_POST[\'title\']),
\'post_status\' => \'private\',
\'post_title\' => $_POST[\'title\'],
\'post_type\' => \'film\',
\'tax_input\' => array(
\'film_category\' => array(
$_POST[\'category\']
)
)
);
$new_film_id = wp_insert_post($post);
wp_set_object_terms($new_film_id, $_POST[\'category\'], \'film_category\', true);
在我的前端表单中,我有包含电影类别的“类别”选择字段。我尝试添加选项值,如术语ID、术语名称和术语Slug,但没有效果。
新帖子是在我的自定义帖子类型中生成的,但我无法指定电影类别。
有什么想法吗?