我想添加一个post_meta (test_meta_1234)字段到一个现有的CPT (组织者)的外部插件。
对于register_meta(),它不起作用。但是,我可以使用register_taxonomy()在同一个CPT中添加一个分类法。
代码示例:
register_meta('post', 'test_meta_1234', array(
'object_subtype' => 'organizer',
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'description' => 'Test Meta 1234',
)
);
register_taxonomy(
'genre',
'organizer',
array(
'label' => __( 'Genre' ),
'rewrite' => array( 'slug' => 'genre' ),
'hierarchical' => true,
)
);
$otherPostTypesFull = new stdClass();
$otherPostTypes = get_post_types();
foreach($otherPostTypes as $postType => $postTypeSlug){
$args = array(
'post_type' => $postTypeSlug,
'numberposts' => -1
);
foreach(get_posts( $args ) as $faPosts){
$otherPostTypesFull->$postTypeSlug->post_meta = get_post_custom($faPosts->ID);
$otherPostTypesFull->$postTypeSlug->taxonomies = get_object_taxonomies( $postTypeSlug, 'objects' );
}
}
var_dump($otherPostTypesFull);分类法是添加到CPT中的,而不是post_meta (test_meta_1234)。
为什么我不能用post_meta ()来查看get_post_custom字段?
更新1:
CPT不支持“自定义字段”,所以现在首先检查并添加以下内容:
`if(!post_type_supports( 'organizer', 'custom-fields' )){
add_post_type_support( 'organizer', 'custom-fields' );
}`自定义字段“test_meta_1234”尚未注册。为什么要这样?
发布于 2019-09-05 23:06:12
无法注册自定义字段(post_meta)。
register_meta只与REST一起工作。
发布于 2019-08-27 19:21:43
仅仅使用https://wordpress.org/plugins/advanced-custom-fields/可能要容易得多
如果没有,请查看这个职位,它将指向此页
https://wordpress.stackexchange.com/questions/345961
复制相似问题